I've modified my .htaccess file to force trailing slashes on all the pages but I'm wondering how to remove the trailing slash in a particular url page? Need help. For example:
abc.com/test successfully redirects to abc.com/test/
But I want to remove that force trailing slash in a particular url of the site,
abc.com/demo/ should redirect to abc.com/demo
Here what I have done so far:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/demo/
RewriteRule ^(.*)([^/])$ /$1$2/ [L,R=301]
Update:
To remove trailing slashes for multiple urls what would be the code, eg abc.com/demo2/ abc.com/demo3/ abc.com/demo4/ etc.. Any suggestions?
Try :
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/demo/ [NC]
RewriteRule ^demo/$ /demo [L,R]
RewriteCond %{REQUEST_URI} !^/demo [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)([^/])$ /$1$2/ [L,R=301]
Related
Last week I have posted something similar like the question whereas I wanted to know how to remove trailing slash using .htaccess for a particular page and rest pages will be redirect with trailing slash.
abc.com/demo/ should redirect to abc.com/demo
And I got the solution as following.
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/demo/ [NC]
RewriteRule ^demo/$ /demo [L,R]
RewriteCond %{REQUEST_URI} !^/demo [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)([^/])$ /$1$2/ [L,R=301]
But now I need for multiple urls. eg. demo2, demo3, demo4 etc, so in those scenario how will be the code, need suggestion.
abc.com/demo2/ redirect to abc.com/demo2
abc.com/demo3/ redirect to abc.com/demo3
abc.com/demo4/ redirect to abc.com/demo4
To remove traling slash from multiple uris ,you could a regular expression capture-group that matches more then one values at one time something like the following :
RewriteCond %{REQUEST_URI} ^/(demo1|demo2|demo3|demo4)/$ [NC]
RewriteRule /$ /%1 [L,R]
The %1 in the above rule is a RewriteCond backreference and it holds the value matched inside
(demo1|demo2|......) regex pattern ie: demo1 .
You can use the following htaccess to remove and add traling slashes .
RewriteEngine on
# remove traling slash from spacifc uris
RewriteCond %{REQUEST_URI} ^/(demo1|demo2|demo3|demo4)/$ [NC]
RewriteRule /$ /%1 [L,R]
# add traling slash to other uris except the spacifc ones
RewriteCond %{REQUEST_URI} !^/(demo1|demo2|demo3|demo4)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)([^/])$ /$1$2/ [L,R=301]
Make sure to clear your browser cache before testing this.
I have setup my .htaccess to add a trailing slash to each URL, but having done this I can see that my Google Analytics conversions (goal type is 'Destination') do not work.
The page I'm trying to track is:
/thank-you/contact/
And the trailing slash gets added by .htaccess if not already there. In GA I have told it to track 'Begins with' /thank-you/contact, 'Equals /thank-you/contact/' etc. Nothing works.
If I comment out the .htaccess rule that adds the trailing slash the conversion tracking immediately starts working again. Have I got some kind of bad config in my .htaccess?
RewriteEngine On
# add trailing slash
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !index.php
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://mydomain.co.uk/$1/ [L,R=301]
#remove www
RewriteCond %{HTTP_HOST} ^www.mydomain.co.uk$ [NC]
RewriteRule ^(.*)$ http://mydomain.co.uk/$1 [R=301,L]
#remove index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
Try regular expression condition with goal settings
thank-you\/contact.*
trailing .* would match or zero and unlimited characters
I am writing code in htaccess to add trailing slashes in url, also applying redirection.
My code is-
#Add slashes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://example.com/$1/ [L,R=301]
Redirect 301 /social-media-consultant http://example.com/seo-consultant/
Redirect 301 /uiux-developers http://example.com/graphics-designer/
Now, problem is that when I write code for adding slashes then redirection stops working.
Change order of your rules
Use only mod_rewrite based rules.
Have it this way:
RewriteEngine On
RewriteRule ^social-media-consultant$ http://example.com/seo-consultant/ [L,NC,NE,R=301]
RewriteRule ^uiux-developers$ http://example.com/graphics-designer/ [L,NC,NE,R=301]
#Add slashes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^ http://example.com/%{REQUEST_URI}/ [L,R=301,NE]
Make sure to clear your browser cache before testing this change.
I am amending my htaccess file to achieve non-www to www (this is working) plus removing the trailing slash at the end of the URL, e.g.:
www.domain.bc.ca/club/ ---> www.domain.bc.ca/club
www.domain.bc.ca/club/index.html/ ---> www.domain.bc.ca/club/index.html
The portion of the htaccess file is below - the Force www bit is working; the Remove trailing slash bit is not. Help! Many thanks, Amanda.
# Force www.
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^domain\.bc\.ca$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
#
# Remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^(.+)/$ /$1 [R=301,L]
I'm going to go out on a limb and guess that you're trying to access a directory when this happens. In your example, the "club" seems to be a directory and when you redirect /club/ to /club, a module called mod_dir will redirect it back to having the trailing slash again. There's a really good reason for this, because if the trailing slash is missing for a directory, the directory's contents will be displayed instead of the index file. That means if you were able to go to www.domain.com/club (without the trailing slash), you'd see all the contents of the club directory instead of the club/index.html file.
If that's ok with you, then you can turn off mod_dir by adding this to your htaccess file:
DirectorySlash Off
But then you'd need to internally add the slash back in:
DirectorySlash Off
# Force www.
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^domain\.bc\.ca$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
#
# Remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^(.+)/$ /$1 [R=301,L]
# Add the slash back
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+[^/])$ /$1/ [L]
I've got the following rewrite rules in my .htaccess, the first 4 lines are supposed to deal with allowing to access the site without index.php, and works fine, until I add the last bit which I'm trying to use to remove trailing slashes from the sites URLs.
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5})$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1 !^(uploads|cache|themes|default|admin\.php|favicon\.ico|robots\.txt|index\.php) [NC]
RewriteRule ^(.*)$ /index.php/$1
# Remove trailing slashes
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]
When I add the last line, and I visit the root of my site, the index.php part gets appended to the URL, why is this?
When I add the last line, and I visit the root of my site, the index.php part gets appended to the URL, why is this?
This is because the rules are applied sequentially. You want the redirect to happen before you route stuff to /index.php. Just swap those rules around:
# Remove trailing slashes
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5})$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1 !^(uploads|cache|themes|default|admin\.php|favicon\.ico|robots\.txt|index\.php) [NC]
RewriteRule ^(.*)$ /index.php/$1 [L]
Following rules are work for me.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
#Removing trailing slash
RewriteRule ^(.*)/$ /$1 [L,R]
#Removing index.php
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=302,NE,L]