I added this line to my .htaccess file to remove trailing backslashes:
RedirectMatch 301 ^(.*)/$ /$1
but it causes the backslash to move to the back of the sub-directory (in front of the domain).
so if I type: localhost/subfolder/
into the address bar and press enter the result is this:
localhost//subfolder
I have also tried
RewriteRule ^(.+)/$ /$1 [R=301,L]
but this had the same issue (and others). Anyone know why this happens.Thank you for reading
p.s. I am using apache2.4.4
Since URI in RedirectMatch starts with a leading slash, you can use this rule:
RedirectMatch 301 ^(.*)/$ $1
But better is to use mod_rewrite rule to exclude directories from this rule:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteRule ^ %1 [R=301,NE,L]
Related
Does it have an access code that killed the slash at the end of the URL?
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/$ /$1 [NE,R=302,L]
How to do something like that:
lala.php/
to
lala.php
To remove trailing slash after .php you can use this simple rule:
RewriteRule ^(.+\.php)/$ /$1 [L,NC,R=301]
In my .htaccess file, I'm using the code:
RewriteEngine on
RewriteRule ^learn/(.*?)/(.*?)/ /learn.php?lang=$1&topic=$2
RewriteRule ^videos/(.*?)/(.*?)/ /video.php?lang=$1&topic=$2
which works fine. But it works on http://domain.com/learn/v1 and http://domain.com/learn/v1/ (notice the slash change).
I want to redirect the non-slash version to slash version maintaining the internal redirect above. I tried to add anothe RewriteRule to do that but then it gives me 404.
Any help would be appreciated.
Try:
RewriteEngine on
## Adding a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=302]
# internal rewrites
RewriteRule ^learn/(.*)/(.*)/$ learn.php?lang=$1&topic=$2 [L,QSA]
RewriteRule ^videos/(.*)/(.*)/$ video.php?lang=$1&topic=$2 [L,QSA]
When user enters url http://localhost/systems/admin in address bar I need to redirect user to page http://localhost/systems/admin/login/
It works fine when enter /admin
It does not work properly when I enter /admin/ (note trailing slashes), it redirects me to some other page http://localhost/systems/user/login but I want it to redirect me to same page i.e. http://localhost/systems/admin/login/
Below is my entire .htaccess code
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /systems/
RewriteRule ^admin$ http://localhost/systems/admin/login/ [R=301,L]
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
</IfModule>
With your .htaccess configuration you are redirecting only /admin to /admin/login/.
To support a possible trailing slash you should go for something like:
RewriteRule ^admin/?$ http://localhost/systems/admin/login/ [R=301,L]
Generally speaking I would stick to only one notation (either with or without the trailing slash) and add a rule that always corrects wrong notations (i.e. adding or removing the trailing slash). A rule to remove the trailing slash could be:
RewriteRule ^(.*)/$ /$1 [L,R=301]
With such a rule you can stick to your original rewrite rule. In this case, however, you should always take care of returning addresses without the trailing slash in order to avoid rewriting when not necessary:
RewriteRule ^admin$ http://localhost/systems/admin/login [R=301,L]
I have content that can display with a double-slash:
domain.com/folder/name//
Obviously this is not ideal.
I want to create a .htaccess 301 rewrite that removes the additional trailing slash:
domain.com/folder/name/
I came-up with:
RewriteRule /(.*)/(.*)// /$1/$2/ [R=301,L]
Though no-dice.
You can't match // in RewriteRule since Apache strips it to single there.
Use RewriteCond instead:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/+(.*?)/+(/.*)$
RewriteRule ^ /%1%2 [R=302,L,NE]
I've got an issue when I'm trying to add a trailing slash to non existent files. Here is my rewrite rules
# remove www from url
RewriteCond %{HTTP_HOST} ^www.goautohub.com [NC]
RewriteRule ^(.*)$ http://goautohub.com/$1 [L,R=301]
#rewrite news/article name
RewriteRule ^news/([^/]*)/$ news.php?viewnews=$1 [NC,L]
#remove index from url
RewriteRule ^index\.php/?$ / [L,R=301,NC]
#remove php from url
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
The only thing left right now I want to do is rewrite this url
/news/mustang-cobra-model-highlights
to
/news/mustang-cobra-model-highlights/
If I use use something like
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/])$ /$1/ [L,R=301]
which I found from Force trailing slash at end of rewritten query string it works but it screws up all my other ones it there is already a trailing slash. What it does it adds
/.php/ to the end.
I figure I need a way to limit that to just the news page but I can't seem to get the rule right.
The followin rewrite rule should work:
RewriteRule ^/news/(.*)/$ /news/$1 [NC,L]