When I type a url, a trailing slash is automatically added.
Why?
For example, I type:
example.com/path
and it becomes:
example.com/path/
My .htaccess file:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
AddType 'text/css; charset=UTF-8' css
</IfModule>
If your URL points to a directory then Apache's mod_dir module automatically adds a trailing slash for security reasons.
Apache mod_dir reference on directory slash
Please note that you can change this behavior by placing this directive:
DirectorySlash Off
However keep in mind this security warning.
Security Warning
Turning off the trailing slash redirect may result in an information disclosure. Consider a situation where mod_autoindex is active (Options +Indexes) and DirectoryIndex is set to a valid resource (say, index.html) and there's no other special handler defined for that URL. In this case a request with a trailing slash would show the index.html file. But a request without trailing slash would list the directory contents.
To remove a trailing slash you can just use:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [R=301,L]
Related
I am currently building a PHP-based portfolio site without any frameworks whatsoever. I have created an index.php file in the root and a lot of folders namely /about, /contact, /portfolio and the like. Within each of those, I have a separate index.php file
I created a .htaccess file and in it, I have this code...
Options +FollowSymLinks
Options -Indexes
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
For some reason when I visit my site at example.com/index.php or example.com/about/index.php the .htaccess file is not working and removing it.
Any ideas why?
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
If the intention with these directives is to remove index.php from the requested URL then you are using the wrong rules! This rule would route a request for /something to index.php/something (passing the requested URL-path as path-info to index.php) providing /something does not map to a file or directory.
You have presumably structured your URLs so they map to filesystem directories from which the DirectoryIndex is served, so the above directives would seem to be entirely redundant. (?)
To remove index.php from the end of any URL
To remove index.php (the DirectoryIndex) from any URL you would need to do something like the following instead:
RewriteRule ^(.+/)?index\.php$ /$1 [R=301,L]
Test first with a 302 (temporary) redirect to avoid potential caching issues.
To clarify...
you must not be linking internally to /index.php or /about/index.php. The above directive is only for when a user or external site erroneously requests the index.php file directly.
And include trailing slashes on your internal links. eg. you should be internally linking to /about/ and /contact/ etc. (not /about or /contact), otherwise mod_dir will implicitly issue a 301 redirect to append the trailing slash.
It took a while and thanks for the suggestions by Mr White this is my solution..
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ %1 [R=301,L]
Works perfectly now.
Thank you for everyone who took the time out to help me on this.
I want to remove the trailing slash so that
https://www.hooklust.com/oo.a/
looks like
https://www.hooklust.com/oo.a
(the dir oo.a contains an index.php file)
I found the following tutorial for removing trailing slashes from URLs, but I still end up with a trailing slash:
https://www.danielmorell.com/guides/htaccess-seo/redirects/https-www-and-trailing-slash
The code:
#### Force HTTPS://WWW and remove trailing / from files ####
## Turn on rewrite engine
RewriteEngine on
# Remove trailing slash from non-filepath urls
RewriteCond %{REQUEST_URI} /(.+)/$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ https://www.example.com/%1 [R=301,L]
# Include trailing slash on directory
RewriteCond %{REQUEST_URI} !(.+)/$
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+)$ https://www.example.com/$1/ [R=301,L]
# Force HTTPS and WWW
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [OR,NC]
RewriteCond %{https} off
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
I took everything else out of the htaccess file to make sure nothing else was adding the slash, but still the same thing.
Why would the slash still be there?
Thank you
Here's my .htaccess file
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
I need this www.domainname.com/folder-name/
to rewrite/redirect to this
www.domainname.com/folder-name/index.html
without screwing up what's already in htaccess.
So i'm clear...when it's all said and done i want www.domainname.com/folder-name/ to display in the URL bar, but I want the content from index.html to be rendered.
You have a problem in that if you go to www.domainname.com/folder-name/, your first rewrite rule will redirect you to www.domainname.com/folder-name, removing the trailing slash. Assuming you haven't turned off DirectorySlash, mod_dir will redirect this back to www.domainname.com/folder-name/, which then triggers the rewrite rule, which triggers mod_dir, etc. If you've turned off DirectorySlash, that means you've exposed an information disclosure bug (or "feature") which will list the contents of the folder-name folder even if index.html exists.
So if you really want to be able to go to www.domainname.com/folder-name/ and not lose the trailing slash, then you need to add a condition to your first rule and then simply allow mod_dir to do its thing:
DirectoryIndex index.html
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
If you've mistyped and you actually want all the trailing slashes to get removed including for folders, leaking the folder contents and all, then you've got to do a bunch of other things. Essentially, you have to do what mod_dir normall does for you anyways and make sure DirectorySlash is turned off (still dangerous, as it could expose the file listings of all your folders):
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
DirectorySlash Off
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{DOCUMENT_ROOT}/$1/index.html -f
RewriteRule ^(.*[^/])$ /$1/index.html [L]
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
This is my current .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
# Force https
RewriteCond %{SERVER_PORT} !^443$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
Header set X-UA-Compatible "IE=Edge,chrome=1"
# block text files in the content folder from being accessed directly
RewriteRule ^content/(.*)\.(txt|md|mdown)$ error [R=301,L]
# make site links work
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php [L]
</IfModule>
What can I do to force a trailing slash when there isn't one? For instance, my current URLs look like this:
https://amemoirproject.com/chapters/quarter-after
I'd like to force a trailing slash so they look like this:
https://amemoirproject.com/chapters/quarter-after/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/])$ http://%{HTTP_HOST}/$1/ [L,R=301]
The RewriteCond makes sure that there isn't any files with that name. If there is not, it adds the trailing slash.
The following is my directory structure
Root/
index.php
contact.php
projects.php
/index.php
/project1.php
/project2.php
I have rewrites in place to remove the .php extension from all file names. It works perfectly fine and I can access www.website.com/projects/project2.php from www.website.com/projects/project2
I also want to be able to access www.website.com/projects/index.php as www.website.com/projects
I have managed to write a rule which rewrites the url to www.website.com/projects/ when i type www.website.com/projects
However, I am not being able to get rid of the last trailing slash.
Please note that I do not really understand much of this. Most of it is from what I have found on the internet. I have looked around a lot but not got anything to work till now.
Here is the code:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^projects$ /projects/index.php [L,E=LOOP:1]
This is caused by mod_dir and the DirectorySlash directive. It will automatically 301 redirect requests for a directory that's missing the trailing slash. This fixes an information disclosure security concern (described in the above link) which lists the directory contents even when there's an index file (e.g. index.php). So if you turn this functionality off, be very careful about your directories. If you've got directory indexing turned off, then that's not so much of a concern.
You can turn of directory slashes using:
DirectorySlash Off
You can turn off directory indexing using the Options:
Options -Indexes
And then, you need to have your projects rule before your php extension rule:
Options +FollowSymLinks -MultiViews -Indexes
DirectorySlash Off
RewriteEngine on
RewriteRule ^projects$ /projects/index.php [L,E=LOOP:1]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]
This come in handy if you want to remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)$
RewriteRule ^(.*)$ https://mydomainname.com/$1 [R=301,L]
The explanation for this rule is the same as it is for when we want to add a trailing slash, just in reverse. We can also specify specific directories that we don't want apply this rule to.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !directory/(.*)$
RewriteCond %{REQUEST_URI} !(.*)$
RewriteRule ^(.*)$ https://mydomainname.com/$1 [R=301,L]
Source