Removing trailing slash from ALL URLs in site - .htaccess

I'm kind of new to the whole .htaccess thing and I've been trying to modify it so that none of my links will have trailing slashes at the end of their respective URLs. My website is filmblurb.org.
The code for the .htaccess where Wordpress begins and ends looks like this:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
I would appreciate it if someone can lead me in the right direction on how to fix this. Thanks.

You can add a RewriteRule to eliminate the trailing slash:
RewriteRule ^(.*)/$ $1 [R=301,L]

The issue is not caused by .htaccess, but rather by a combination of wordpress permalinks and .htaccess.
Login to your site and navigate to the permalinks, then if you aren't using the custom structure option, switch to it and make sure to not have a trailing slash at the end:
/%category%/%postname%
Then add this to your .htaccess file, above the
RedirectMatch 301 ^(.*)/$ /$1
That is better than using rewrite since it's a redirect and not a rewrite.
If that still doesn't work then I recommend you install the yoast seo plugin and there is a setting in it to do this.

This works for me; removing all trailing slashes from all routes while emphasizing that REQUEST_URI starts with a slash (at least in .htaccess files):
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /(.*)/$
RewriteRule ^ /%1 [R=301,L]
Just don't use %{REQUEST_URI} (.*)/$. Because in the root directory REQUEST_URI equals /, the leading slash, and it would be misinterpreted as a trailing slash.
SOURCE: https://stackoverflow.com/a/27264788/2732184

Related

How to clean url to accept ending slash or not with .htaccess

How do I clean URL
http://localhost/mysitedirectory/contacts.php
to look like both:
http://localhost/mysitedirectory/contacts
http://localhost/mysitedirectory/contacts/
I want this because a user may add the ending trailing slash or leave it so I want .htaccess to configure such that it works both ways.
I noticed when I add the / at the end of the contacts it says "OBJECT NOT FOUND"...Error 404
How do I make it work even if I add the slash or not?
this is what I tried:
RewriteEngine On
Options -Indexes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^contacts$ contacts.php [L,QSA,NC]
RewriteRule ^home$ index.php [L,QSA,NC]
CBroe answered in a comment section:
^contacts/?$ - slash at the end, made optional by the following quantifier question mark.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^contacts$ contacts.php [L,QSA,NC]
RewriteRule ^home$ index.php [L,QSA,NC]
The two RewriteCond directives (filesystem checks) are entirely superfluous and should be removed. (As are the QSA flags on the RewriteRule directives.)
However, instead of allowing both /contacts and /contacts/ (strictly speaking two different URLs) resolve to the same content (which would necessitate the need for a rel="canonical" tag) you should externally redirect from one to the other (canonical) URL.
By the look of it, the URL without the trailing slash is canonical, so remove the trailing slash with an external redirect. The only thing to be careful of is that you should not remove the trailing slash from physical directories (which would ordinarily cause a redirect loop).
For example:
Options -Indexes -MultiViews
RewriteEngine On
# Canonical redirect to remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.+)/$ /$1 [R=301,L]
# Internal rewrites
RewriteRule ^contacts$ contacts.php [L,NC]
RewriteRule ^home$ index.php [L,NC]
The NC flag on the RewriteRule directives makes contacts, CONTACTS and CoNtAcTs all resolve to the same content. (So the rel="canonical" tag is still required to resolve this ambiguity.)

htaccess - URL 'folders' always show 'folders/?link=folders'

I'm sorry if my question is unclear. I rewrite rule url by htaccess and it worked. So i have a problem when i try to link path of the existing folder.
My problem:
Redirect url: 'http://localhost/folders'
But it display: 'http://localhost/folders/?link=folder'
So, I don't want it show '?link=folder'. It not show '?link=folder' with Redirect url: 'http://localhost/folders'
My htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^$ index.php [L]
RewriteRule ^([^/]*)/$ index.php?link=$1&%{QUERY_STRING} [L]
RewriteRule ^([^/]*)/([^/]*)/$ index.php?link=$1&action=$2&%{QUERY_STRING} [L]
RewriteRule ^([^/]*)/([0-9]+)/([^/]*)/$ index.php?link=$1&id=$2&action=$3&%{QUERY_STRING} [L]
RewriteRule ^([^/]*)$ index.php?link=$1&%{QUERY_STRING} [L]
RewriteRule ^([^/]*)/([^/]*)$ index.php?link=$1&action=$2&%{QUERY_STRING} [L]
RewriteRule ^([^/]*)/([0-9]+)/([^/]*)$ index.php?link=$1&id=$2&action=$3&%{QUERY_STRING} [L]
RewriteRule !^(public/*|folder/*|index\.php) [NC,F]
Please, someone tell me how to fix. I'm sorry if my english is bad.
Your problem maybe to do with the DirectorySlash. If "folders" is a physical directory on the filesystem and you request http://localhost/folders (no trailing slash) then mod_dir "fixes" the URL by 301 redirecting to http://localhost/folders/ (with a trailing slash).
This mod_dir behaviour conflicts with the following rule that appends the ?link=folders quesy string.
RewriteRule ^([^/]*)$ index.php?link=$1&%{QUERY_STRING} [L]
This "rewrite" will be turned into an external "redirect" by mod_dir.
You can try preventing mod_dir from appending the trailing slash on directories with the following directive at the top of your .htaccess file:
DirectorySlash Off
However, you now need to manage all the trailing slashes yourself, which may not be trivial.
However, instead of the above, I would simply include a trailing slash on the URL to begin with and only match URLs that have the trailing slash, rather than both (you are duplicating your directives).

Why does my .htaccess append the query string to the URL?

Basically I have my website set up the following way:
mysite/site -Goes to the main index.php file
mysite/site/asdf -Goes to the main index.php file with a subpage
mysite/site/admin -Goes to an admin panel with various subpages
But the issue I'm running into is when I go to mysite/site/admin without the trailing slash, it appends /?admin=1 to the URL. Same if I go to mysite/site/admin/pages without the trailing slash, it appends /?admin=1&page=pages to the URL. I want it to not append these query strings.
If I add the trailing slash, the query string does not get appended. If I go to mysite/site/admin/pages/edit without the trailing slash, it doesn't append the query string, so it seems just the first 2 levels do this.
This is confusing and I don't understand it at all. I've tried lots of various things from googling and searching this site but nothing has worked. I'm a newbie to this .htaccess stuff. Here's what my .htaccess file looks like:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^/?admin/users/([\-_A-Za-z0-9]+)/?$ admin/index.php?admin=1&page=users&subpage=view&user=$1 [L]
RewriteRule ^/?admin/users/create/?$ admin/index.php?admin=1&page=users&subpage=create [L]
RewriteRule ^/?admin/([\-_A-Za-z0-9]+)/([\-_A-Za-z0-9]+)/([\-_A-Za-z0-9]+)/?$ admin/index.php?admin=1&page=$1&subpage=$2&id=$3 [L]
RewriteRule ^/?admin/([\-_A-Za-z0-9]+)/([\-_A-Za-z0-9]+)/?$ admin/index.php?admin=1&page=$1&subpage=$2 [L]
RewriteRule ^/?admin/([\-_A-Za-z0-9]+)/?$ admin/index.php?admin=1&page=$1 [L]
RewriteRule ^/?admin/?$ admin/index.php?admin=1 [L]
RewriteRule ^/?([\-_A-Za-z0-9]+)/?$ index.php?page=$1 [L]
</IfModule>
Options -Indexes
Can anybody tell me what I'm doing wrong or how to fix this?
That is because /site/admin/pages is a valid directory and without trailing slash Apache's mod_dir module redirects the URL to one with a trailing slash.
To fix you can use:
RewriteEngine On
# add a trailing slash to directories
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*?[^/])$ %{REQUEST_URI}/ [L,R=302]
RewriteRule ^/?admin/users/([-\w]+)/?$ admin/index.php?admin=1&page=users&subpage=view&user=$1 [L]
RewriteRule ^/?admin/users/create/?$ admin/index.php?admin=1&page=users&subpage=create [L]
RewriteRule ^/?admin/([-\w]+)/([-\w]+)/([-\w]+)/?$ admin/index.php?admin=1&page=$1&subpage=$2&id=$3 [L]
RewriteRule ^/?admin/([-\w]+)/([-\w]+)/?$ admin/index.php?admin=1&page=$1&subpage=$2 [L]
RewriteRule ^/?admin/([-\w]+)/?$ admin/index.php?admin=1&page=$1 [L]
RewriteRule ^/?admin/?$ admin/index.php?admin=1 [L]
RewriteRule ^/?([-\w]+)/?$ index.php?page=$1 [L]

Trailing slash issue using mod_rewrite for SEO friendly URL

I consider myself reasonably competent with PHP. I am, however, completely and totally lost when it comes to mod_rewrite.
I have a URL structure that works like the following:
http://site/something/something-else/the-actual-page/
that redirects to:
http://site/index.php?page=the-actual-page
It's only ever the final 'folder' that is passed to the script. The preceding 'folders' (if any) are for SEO and structure purposes.
If there is a preceding folder "promotion" then it redirects to a separate file. This is along the lines of:
http://site/promotion/campaign-name/
redirecting to
http://site/promotion.php?campaign=campaign-name
I'm using the following code to achieve this:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^promotion/(.*)/$ promotion.php?params=$1 [L]
RewriteRule ^(.*) index.php?page=$1 [L]
</IfModule>
This works as intended, with links redirecting properly EXCEPT when there is no trailing slash. For example
http://site/something/thepage/
will work, whilst
http://site/something/thepage
will not.
To solve this problem I'm attempting to set up a 301 that redirects any URI without a trailing slash to a URI with a trailing slash.
The code below (placed above the other rules) works to a degree, but I lose folder data.
Code:
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [L,R=301]
The problem?
http://site/something/thepage
redirects to
http://site/thepage/
I'm afraid all the googling in the world is not helping me, as I cannot wrap my brain around mod_rewrite at all!
Appreciate any help.
You'll be better off using this:
RewriteEngine On
RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
# ... your other rerites
If you'd like to reverse it and strip the trailing slash instead, then use this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [R=301,L]
You'd then need to change your rewrite accordingly:
RewriteRule ^promotion/(.*)$ /promotion.php?params=$1 [L]

.htaccess: clean URL with parameter "images" doesn't work

I've just started trying to figure out how to make clean URLs with .htaccess but it seems I've run into a wall straight away... I've got the following simple .htaccess:
RewriteEngine on
RewriteRule ^([a-zA-Z0-9-]+)$ index.php?id=$1 [L]
RewriteRule ^([a-zA-Z0-9-]+)/$ index.php?id=$1 [L]
It mostly works as expected: mysite.com/somepage goes to mysite.com/index.asp?id=somepage etc.
Except when the id is 'images'. When I try mysite.com/images it displays: mysite.com/images/?id=images (though the page still loads fine)
By the way mysite.com/images/ (with a slash) works ok. I've been searching for a solution for the last few days and that's the best I could get.
Since images is a directory Apache's mod_dir module adds a trailing slash after your rewrite rules. You can turn this feature off and add a trailing slash yourself:
DirectorySlash Off
RewriteEngine on
# add a trailing slash
RewriteCond %{DOCUMENT_ROOT}/$1 -d
RewriteRule ^(.*?[^/])$ %{REQUEST_URI}/ [L,R=302]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9-]+)/?$ index.php?id=$1 [L,QSA]

Resources