Remove trailing slashes in URLs - .htaccess

I want to remove all trailing slashes for ALL URLs ending in .html. I already have this code which is supposed to remove them for all URLs:
RewriteRule ^(.*)/$ $1 [R=301,L]
But I want to apply it only to URLs ending in .html, such that:
http://example.com/a/b.html/ > http://example.com/a/b.html
http://www.example.com/ccc.html///// > http://www.exaxmple.com/ccc.html
Note that for some reason I am seeing multiple slashes at the end of some URLs, and I want these removed in a single redirect.
Can anyone help?

Figured it out:
RewriteRule ^(.*)(.html)/+$ $1$2 [R=301,L]

Related

Htaccess to 301 redirect no ending slash to ending slash

I have 2 domains that are displaying the same information. I would like to have the one that does not end in a slash 301 redirect to the one that does.
http://domain.com/locations/texas
http://domain.com/locations/texas/
Current Rewrite Rule:
RewriteRule ^locations/([a-zA-Z0-9_-]+)/?$ search.php?type=location&slug=$1 [L]
Change the rule you have no and remove the ? making the trailing slash a requirement:
RewriteRule ^locations/([a-zA-Z0-9_-]+)/$ search.php?type=location&slug=$1 [L]
Then add this (before or after):
RewriteRule ^locations/([a-zA-Z0-9_-]+)$ /locations/$1/ [L,R=301]
to redirect the browser when a request is made without the trailing slash.

Htaccess rewrite index with trailing slash or without

I am currently having a problem with my index url rewrite in my .htaccess file, I know if I use
RewriteRule ^profile/([^/]*)/?$ /profile.php?x=$1 [L]
I would be able to use www.example.com/profile/get or www.example.com/profile/get/ (with or without trailing slash)
But I would like www.example.com/get what I have so far is
RewriteRule ^([^/]*)\/$ /index.php?x=$1 [L]
But if I put a ? before the $ it errors any answers welcome
Making the trailing slash optional will lead to an infinite loop, since [^/]* will match anything that doesn't include a /, ie it would also match index.php?x=get
You can avoid this by making the rule apply conditionally, for example by testing the reqeust URI:
RewriteCond %{REQUEST_URI} !^/index\.php.*
RewriteRule ^([^/]*)\/?$ /index.php?x=$1 [L]
That way the rule can only apply in case the request URI doesn't start with /index.php

htaccess rewrite rules for directories

I am having trouble with some rewrite rules on an updated site...I am trying to redirect requests for old directories to new pages, like this:
http://www.mysite.com/olddirectory --> http://www.mysite.com/this-is-the-new-page
the following rule works, but not with a trailing slash on the directory:
ie: /olddirectory redirects correctly, but /olddirectory/ doesn't
RewriteRule ^olddirectory$ this-is-the-new-page [R=301,NC,L]
Any ideas on how to get it to recognise the trailing slash on the dir?
The following rule will do redirect with or without trailing slash:
RewriteRule ^olddirectory/?$ /this-is-the-new-page [NC,R=301,L]
The key is to make trailing slash optional, and that's what ? does: /?

Mode Rewrite; with/without trailing slash on end of url?

I basically tried this mode_rewrite rule below. It works with a slash on the end but I want it to work whether it has a trailing slash on the end or not. Basically I want it like this as some people see it as normal with a slash on the end and others don't hence why I want it to work whether it's there or not.
RewriteRule ^signup/register(.[^/]*) /signup/register.php [NC]
Basically it will work like http://localhost/signup/register/ but if I remove the / from the end it gives 404 error.
The subpattern .[^/]* requires at least one arbitrary character. In your case it’s probably that trailing slash.
You should better stick to one writing (either with or without trailing slash) and redirect th wrong writing to the proper, like:
# remove trailing slash
RewriteRule ^(.*)/$ /$1 [L,R=301]
# add trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*[^/]$ /$0/ [L,R=301]
plain old regexes:
RewriteRule ^signup/register/?$ /signup/register.php [NC]
...
RewriteRule ^(url-rewrite)/?$ page.php [NC]
...
The ? after / specifies that there can be none or one / after your url-rewrite, as such it would accept it with or without the trailing /

Force adding slach to the end in RewriteRule

i'm working with mod_rewrite under .htaccess, and i'm trying to fore adding a slach "/" to the end of the url :
interpret this url : http://domain/ABC/DEF like this http://domain/ABC/DEF/
how can i write the rule please ?
To add a trailing slash at the end of Request_uri, You can use the following rule in htaccess :
RewriteEngine On
#If there is already a trailing slash, skip the rule.
RewriteCond %{REQUEST_URI} !/$
#else redirect any request to add a trailing slash
RewriteRule ^(.+)$ /$1/ [L,R]
This automatically adds a trailing slash and changes the url
http://example.com/foo/bar
to
http://example.com/foo/bar/
The RewriteCond is important here to avoid redirect loop error, without this condition a request for /foo redirects to /foo/ on initial iteration, On the second iteration the target url /foo/ matches the Rewrite pattern and redirects /foo/ to /foo/ and causes the loop error.
(Hope ,This helps!)

Resources