removing the forward slash from this re-written url - mod-rewrite

I know this may sound silly little bit, but there is no other way to resolve it. I have an issue with the forward slash /. I want to remove it from the url because it is kind of making some confusion to the browsers giving trusted or un-trusted urls.
This is the code for the main domain:
RewriteCond %{SERVER_PORT} !^443$
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^ /user [R=301,L]
and this is the output of the above code: https://www.mydomain.com/user/
but I want the url redirect to this:
https://www.mydomain.com/user
I also want to remove the forward slash from this url :
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(?:\.\w+|/)$
RewriteRule (.*) /$1/ [R,L]
RewriteCond %{REQUEST_URI} ^(/end)
RewriteRule ^(.*)$ /top/right/left/bottom/$1 [L]
RewriteCond %{REQUEST_URI} ^/top/right/left/bottom
RewriteRule top/right/left/bottom/(end)(/(./*))?$ $1$2 [R,L]
and this is the output of the above code: https://www.mydomain.com/end/
but I want the url redirect to this:
https://www.mydomain.com/end
Last question: why do I have to write www. in my own in some browser like FF or chrome so the redirect make itself to the required url?
All comments are appreciated

Looks like the above rewrites rules are written by me. So optimizing it for URL to not end with a / (https://www.mydomain.com/end)
Replace the whole(from RewriteCond %{ENV:REDIRECT_STATUS} 200 to RewriteRule top/right/left/bottom/(end)(/(./*))?$ $1$2 [R,L]) thing by this:
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
RewriteRule ^(end)(?:/(.*))? /top/right/left/bottom/$1/$2 [L]
RewriteRule ^top/right/left/bottom/(end)(/.*)?$ $1$2 [R,L]
Remember though if you access with a forward slash like this: https://www.mydomain.com/end/ the forward slash will not be removed.
If you want the / to be removed: Replace the last 2 rules by these:
RewriteRule ^(end)(?:/(.+))? /top/right/left/bottom/$1/$2 [L]
RewriteRule ^(end)/$ $1 [R,L]
RewriteRule ^top/right/left/bottom/(end)(/.*)?$ $1$2 [R,L]

Store the url in a variable and perform rtrim to remove trailing slash
$redirectURL = rtrim($redirectURL, "/");

Related

Remove "?" from the query string

I have decided to ditch Wordpress and move to Pico CMS instead.
Now I'm struggling with getting the URL rewriting done correctly.
I have managed to rewrite the query string from eg. https://www.example.org?page to https://www.example.org/page using RewriteRule ^(.*) ?$1 [L], but now I also want it rewritten if there is a query string.
Example:
If https://www.example.org?page is requested it loads just fine, but I want to have it rewritten as https://www.example.org/page - at least in the location bar.
I have tried several variations but to no avail. I suck at regular expressions...
RewriteCond %{QUERY_STRING} !^$
RewriteRule ^\\?/(.*)$ $1
Nope
RewriteRule (^\?$) https://tanghus.net/$1 [NC,R=301,L]
RewriteRule ^.*$ https://tanghus.net/? [NC,R=301,L]
no dice
RewriteCond %{QUERY_STRING} .
RewriteRule ^$ %{QUERY_STRING} [R,L]
Pfff
RewriteCond %{THE_REQUEST} \?\sHTTP [NC]
RewriteRule ^ %{REQUEST_URI} [L,R]
Giving up :(
You can use this Rule
RewriteEngine on
# externally redirect from /?page to /page
RewriteCond %{THE_REQUEST} /\?([^\s]+) [NC]
RewriteRule ^/?$ /%1? [L,R]
# internally map /page to /?page
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?(.+)$ /?$1 [L]

RewriteCond Query String .htaccess

EDIT: I must add something.First of all i want to change url display because of SEO.If i use www for reach my website there is no problem, second link is appears and everything is ok.
But if i remove "www" from link, it changing to first url and i doesn't want that.
I want to change
http://www.mysite.com/index.php?route=epson-claria-uyumlu-yazici-kartus-dolum-murekkebi-500g.html
to
http://www.mysite.com/epson-claria-uyumlu-yazici-kartus-dolum-murekkebi-500g.html
how I can do it?
I tried
RewriteCond %{QUERY_STRING} ^_route_=(.*)$
RewriteRule ^index\.php$ /%1 [R=301,L]
but it is not working.
My .htaccess is
RewriteBase /
RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap [L]
RewriteRule ^googlebase.xml$ index.php?route=feed/google_base [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]
RewriteCond %{QUERY_STRING} ^route=common/home$
RewriteRule ^index\.php$ http://www.mysite.com? [R=301,L]
RewriteCond %{HTTP_HOST} !^www\.mysite\.com$
RewriteRule (.*) http://www.mysite.com/$1 [R=301,L]
Maybe this is what you're looking for:
RewriteRule ^(.*)$ index.php?route=$1 [L]
If you have to visually change the address bar, leave the RewriteRule in place as I described above, and put this in your index.php before any output:
if(isset($_REQUEST['route']))
{
header('Location: '.urlencode($_REQUEST['route']));
}
Initial note: I'm not an Apache guru so don't rely blindly on my answer.
I would first redirect to www. if required
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{SERVER_NAME}/$1 [R=301,L]
Then make it go to the page indicated by the _route_ query variable
# if it is the index page...
RewriteCond %{REQUEST_URI} ^/(index\..+)?$ [NC]
# and if the query string starts with _route_=
RewriteCond %{QUERY_STRING} ^_route_=(.*)$
# redirect
RewriteRule ^(.*)$ http://%{SERVER_NAME}/%1? [R=301,L]
Server variable SERVER_NAME in last line might need to be changed with HTTP_HOST.

Apache mod_rewrite friendly URLs with corresponding 301 redirects

The Problem:
Been spinning my wheels and reading up on this one for awhile and looking for some help now. I'm looking to take a group of non-friendly URLs (there are actually more "groups" but this should me for an example):
domainname.com/?section=zebras
domainname.com/?section=monkeys&id=555
and turn them into friendly URLs, as well as do a 301 on the old versions, so that any old bookmarks (and search engines) will still resolve them. The new format I'm looking for would be:
domainname.com/zebras/
domainname.com/monkeys/555
I'm fully intending to write separate RewriteCond/RewriteRule combinations for each of those scenarios, so I don't necessarily need a super-rule that catches all my scenarios. Oh and this is all in .htaccess.
My Progress:
I was originally getting into a redirect loop because I was just doing two RewriteRules back to back - one for the friendly URL and one for the 301 redirect. Came across my favorite way (so far) around the redirect loop which works (for my scenario #1 at least):
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{QUERY_STRING} ^section=zebras$ [NC]
RewriteRule ^.*$ http://www.domainname.com/zebras/? [R=301,NC,L]
RewriteRule ^zebras/$ /index\.php?section=zebras [NC,L]
However, I'd like to have something that works for more than just "zebras" (for instance, I'd like it to work for "lions" as well), so I'm trying to make that more generic. What I am trying now looks like this:
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{QUERY_STRING} ^section=([a-z]+)$ [NC]
RewriteRule ^section=([a-z]+)$ http://www.domainname.com/$1/? [R=301,NC,L]
RewriteRule ^([a-z]+)/$ /index\.php?section=$1 [NC,L]
However, this doesn't work. I think I have something "not quite right", I just can't tell what it is - there's something I'm missing or formatting incorrectly somewhere. Sorry in advance for the lengthy description, just wanted to be clear.
Do this:
RewriteEngine on
RewriteBase /
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.\w+|/)$
RewriteRule (.*) /$1/ [R,L]
RewriteCond %{QUERY_STRING} ^section=([a-z]+)$ [NC]
RewriteRule ^ /%1/? [R=301,NC,L]
RewriteRule ^([a-z]+)/$ /index\.php?section=$1 [NC,L]
RewriteCond %{QUERY_STRING} ^section=([a-z]+)&id=(\d+)$ [NC]
RewriteRule ^ /%1/%2/? [R=302,NC,L]
RewriteRule ^([a-z]+)/(\d+)/$ /index\.php?section=$1&id=$2 [NC,L]
Description
Prevents looping:
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
Prevents trailing slash problem:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.\w+|/)$
RewriteRule (.*) /$1/ [R,L]
Handles rewrites with only section=([a-z]+) in them:
RewriteCond %{QUERY_STRING} ^section=([a-z]+)$ [NC]
RewriteRule ^ /%1/? [R=302,NC,L]
RewriteRule ^([a-z]+)/$ /index\.php?section=$1 [NC,L]
Handles rewrites with only section=([a-z]+)&id=(\d+) in them:
RewriteCond %{QUERY_STRING} ^section=([a-z]+)&id=(\d+)$ [NC]
RewriteRule ^ /%1/%2/? [R=302,NC,L]
RewriteRule ^([a-z]+)/(\d+)/$ /index\.php?section=$1&id=$2 [NC,L]
mistake in your rules:
section=([a-z]+) is not available in the URI part. So, RewriteRule ^section=([a-z]+)$ never matched.

double slash apache configuration

i'm deploying a ror application and now i have to rewrite the url (in apache) to
add a prefix www to the url
add / to the end of the url
So i took the following approach:
RewriteCond %{REQUEST_URI} ^/[^\.]+[^/]$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [R=301,L]
RewriteCond %{HTTP_HOST} ^foo\.com
RewriteRule ^(.*)$ http://www.foo.com/$1 [R=301,L]
The problem is that it is appending two trailing slash to my url
So for example a resource /question/ask are becoming:
http://foo.com//question/ask
I tried to add the following Rule before all my Rewrite rules to try to remove the double //:
RewriteCond %{REQUEST_URI} ^//
RewriteRule ([^/]*)/+(.*) http://www.foo.com/$1/$2 [R=301,L]
but it didnt work.. any idea to rip off all extras "//" added to the url?
The $1 will include a / at the beginning. You probably want
RewriteCond %{REQUEST_URI} ^/[^\.]+[^/]$
RewriteRule ^(.*)$ http://%{HTTP_HOST}$1/ [R=301,L]
RewriteCond %{HTTP_HOST} ^foo\.com
RewriteRule ^(.*)$ http://www.foo.com$1 [R=301,L]

Mod rewrite most requests to index.php

Options +FollowSymlinks
RewriteEngine On
RewriteBase /
##RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !\.(js|ico|gif|jpg|png|css)$ [NC]
RewriteCond %{REQUEST_FILENAME} !^index.php [NC]
RewriteCond %{REQUEST_FILENAME} !^$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
## RewriteRule ^/([a-zA-Z_+-]+).php$ index.php?p=%1 [R=301]
## RewriteRule ^/?([-a-zA-Z0-9_+]+)$ index.php?p=$1 [R=301,L]
## RewriteRule !\.(js|ico|gif|jpg|png|css)$ index.php?b={REQUEST_FILENAME}
RewriteRule * index.php?p={REQUEST_FILENAME} [R=301,L]
Above you can see my attempts to redirect any request that is not an existing directory, is not index.php and is not a static resource to redirect to index.php?p=resourcename
I am not having any luck. Basically the purpose of this is to redirect static and old urls to new ones as I have just rewritten an old site.
The PHP will handle the redirect logic.
At the moment this code causes an internal server error, I assume because it is caught in a redirect loop. What have I done wrong? My brain is fried after a long day.
Untested, but worth a try :
RewriteRule .* /index.php?p={REQUEST_FILENAME} [R=301,L]
The ".*" part means you want to match 1 or more characters (any of them). And the "/" in front of the "index.php" is probably not mandatory but makes things clearer even if you have the RewriteBase option set to "/" already.
You may also want to add the parameter "QS" between the brackets, to be sure to get the querystring that may be passed with the queries (would be [QS,R=301,L]). Hope this works, and this helps :)
Edit: There's also the "%" in front of "{REQUEST_FILENAME}", as stated by Gumbo.
This code eventually solved my problem.
Thanks for the help, though.
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !index.php
RewriteRule ^/?([-a-zA-Z0-9_+]+).php$ /$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([-a-zA-Z0-9_+]+)$ index.php?p=$1 [L]
You forgot the % in front of the variable (%{REQUEST_FILENAME}) and an expression that should be repeated zero or more times (* is just a quantifier):
RewriteRule .* index.php?p=%{REQUEST_FILENAME} [R=301,L]

Resources