Remove trailing slash using .htaccess for multiple urls - .htaccess

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.

Related

htaccess cannot rewrite to proper URL

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^/articles/([0-9]+)/$ /article-one.html?aid=$1 [L]
from SEO friendly URL eg.
https://example.com/articles/99/aaaa-aabbbb-bb-cccccccc
rewrite to
https://example.com/article-one.html?aid=99
why does it not work?
^/articles/([0-9]+)/$
In a directory context (ie. .htaccess) the URL-path matched by the RewriteRule pattern does not start with a slash. However, this would only match URLs of the form /articles/99/, not /articles/99/aaaa-aabbbb-bb-cccccccc - as stated in your example.
You would need to modify the RewriteRule pattern to something like the following instead:
^articles/([0-9]+)/[\w-]*$
This specifically matches an optional trailing slug of the form aaaa-aabbbb-bb-cccccccc (no trailing slash). If you simply wanted to ignore anything that comes after the numeric path segment then just remove the trailing $ (end-of-string anchor). For example:
^articles/([0-9]+)/

Remove trailing slash using .htaccess for a particular page

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]

Remove index.php AND trailing slashes from URL

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]

Removing query_string from wildcard subdomain rewrite

My goal is to point:
http://xyz.domain.com/abc to http://www.domain.com/dir/file.php?var=xyz
(In other words, anything with http://_____.domain.com/abc would read from http://www.domain.com/dir/file.php?var=_____.)
I used this in my .htaccess file at the root of the website (http://www.domain.com/.htaccess):
RewriteCond %{HTTP_HOST} !^www.domain.com [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([a-z0-9-]+).domain.com [NC]
RewriteRule ^abc/*$ dir/file.php?var=%2 [NC,L]
It worked, but then it automatically redirected to http://xyz.domain.com/abc/?var=xyz when I just wanted http://xyz.domain.com/abc without the query string showing up.
I considered adding a %{QUERY_STRING} line to the above but got stuck when trying to match the two RewriteCond's vars to each other.
Then I found this at http://www.askapache.com/htaccess/modrewrite-tips-tricks.html#Removing_Query_String
RewriteCond %{THE_REQUEST} ^GET\ /.*\;.*\ HTTP/
RewriteCond %{QUERY_STRING} !^$
RewriteRule .* http://www.askapache.com%{REQUEST_URI}? [R=301,L]
...which I'm guessing it's the right code to get rid of something like this on a regular domain? I tried making it suitable for wildcard subdomains by changing it to this:
RewriteCond %{THE_REQUEST} ^GET\ /.*\;.*\ HTTP/
RewriteCond %{QUERY_STRING} !^$
RewriteCond %{HTTP_HOST} !^www.domain.com [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([a-z0-9-]+).domain.com[NC]
RewriteRule .* http://%2.domain.com%{REQUEST_URI}? [R=301,L]
But it doesn't work. SO...can anyone help?
There's a Module called mod_dir that redirects the browser when it thinks you are trying to access a directory but are missing the trailing slash. When I test on my apache it only happens when the directory actually exists, I get redirected with a trailing slash, but the rewrite happens and I see the query string. Try forcing, internally, the trailing slash and when rewriting to dir/file, expressly use a trailing slash:
# Force trailing slash when accessing /abc without one
RewriteRule ^abc$ /abc/ [NC,L]
# these are the same
RewriteCond %{HTTP_HOST} !^www.domain.com [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([a-z0-9-]+).domain.com [NC]
# Match against a trailing slash
RewriteRule ^abc/$ dir/file.php?var=%2 [NC,L]

Force trailing slash

I'm trying to force a trailing slash to my URLs, but I can't make it work the way I want. This is my .htaccess file:
RewriteEngine on
#Force trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !index.php
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ /$1/ [L,R=301]
#Subdomains
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} ^([^/.]+)\.example\.com$
RewriteCond $1/%1 !^([^/]+)/\1$
RewriteRule ^/([^/]+)? /%1%{REQUEST_URI} [L]
#Point everything to page.php
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond $1 !^(.*).(php|css|js|png|jpg|gif|htm|html)$
RewriteRule ^(.*)$ page.php?q=$1 [L,NC]
If I go to "en.example.com/about" I'm redirected to "en.example.com/en/about/", which is an invalid page.
How can I make this work?
The problem here is that the L flag causes a restart of the rewriting process with the rewritten URL (I’ve already told you that, didn’t I?):
Remember, however, that if the RewriteRule generates an internal redirect (which frequently occurs when rewriting in a per-directory context), this will reinject the request and will cause processing to be repeated starting from the first RewriteRule.
Now when /about is requested, the first rule get’s applied and redirects to /about/. The subsequent request of /about/ is then processed, at first the third rule is applied and the URL path is rewritten to /page.php. So far, so good.
But now the internal redirect takes place and the rewriting process is restarted with the new URL path /page.php. This is then fetched by the first rule again and redirected externally to /page.php/.
The second rule shouldn’t be applied at all as the pattern ^/ should never match as the per-directory path prefix is removed before testing the pattern when using mod_rewrite in an .htaccess file:
When using the rewrite engine in .htaccess files the per-directory prefix (which always is the same for a specific directory) is automatically removed for the pattern matching and automatically added after the substitution has been done.
But these rules should work:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .*[^/]$ /$0/ [L,R=301]
RewriteCond %{HTTP_HOST} ^([^/.]+)\.example\.com$ [NC]
RewriteCond %1 !=www [NC]
RewriteCond $0/%1 !^([^/]+)/\1$
RewriteRule ^[^/]* /%1%{REQUEST_URI} [L]
RewriteCond %{HTTP_HOST} !=www.example.com [NC]
RewriteCond $1 !.*\.(php|css|js|png|jpg|gif|htm|html)$
RewriteRule .* page.php?q=$0 [L]

Resources