Removing The Trailing Slash from a directory in htaccess - .htaccess

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

Related

How to remove index.php using htaccess

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.

.htaccess rule not excluding existing files when rule has a slash condition

I am redirecting this type of url
http://www.site.com/windows/games/ or http://www.site.com/windows/games
http://www.site.com/windows/software/ or http://www.site.com/windows/software
to
http://www.site.com/windows/ct.php?ct=games
http://www.site.com/windows/ct.php?ct=software
Site structure
wamp/www/Site/windows/ct.php
I am trying this way its redirecting properly but when url has trailing slash at end its rewriting css, js files too.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^windows/([^/]*)/?$ /Site/windows/ct.php?ct=$1 [L]
i.e
this type of url has css and javascript applied.
http://www.site.com/windows/games
but this type of url has no css and javascript applied. (not working with trailing slash).
http://www.site.com/windows/games/
I have tried several syntax like
RewriteRule ^windows/([^/]+)/?$ /windows/ct.php?ct=$1 [L]
RewriteRule ^windows/(^(.[^/]*)/?)$ /windows/ct.php?ct=$1 [L]
RewriteRule ^windows/([^/]*)/? /windows/ct.php?ct=$1 [L]
RewriteRule ^windows/([^/]+)/? /windows/ct.php?ct=$1 [L]
but it didn't work.
Complete .htaccess
Options +FollowSymlinks -MultiViews
RewriteEngine on
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^windows/([^/]*)/?$ /Site/windows/ct.php?ct=$1 [L]
RewriteRule ^[^/]*/[^/]*/(.*\.html) /Site/error/400/ [L]
RewriteRule ^(error) - [L]
RewriteRule ^sitemap\.xml$ sitemap.php [L]
RewriteRule ^rss/(.*?)\.xml$ rss/$1.php [L]
</IfModule>
ErrorDocument 404 /Site/error/404/
Please see and suggest any possible way to do it.
Thanks.
The issue here is that your relative paths break when you add a trailing slash as it introduces two-levels of directory structure while the CSS/JS file paths come out only once ... So, your RewriteRule is actually firing for CSS/JS files as well when resolving 404s for paths like /windows/global/js/js.js. But, the scripts still fail to work because they aren't under /Site and that's where your rule is serving the request from.
So, to fix things without touching the relative URLs or resource locations; add another rule as follows:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^windows/([^/]*)/?$ /Site/windows/ct.php?ct=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^windows/(global/.*)$ /$1 [L] # handles css|js files
The rest of your .htaccess (of course) remains the same.
I use the following rule in .htaccess, but this may be too general:
RewriteRule ^q/([^/]+)/([^/]+)(\/?)$ /path/to/q.php?type=$1&q=$2&{QUERY_STRING} [NC]
This may be better for you:
RewriteRule ^windows(\/?)$ /path/to/windows.php?{QUERY_STRING} [NC]
RewriteRule ^windows/([^/]+)(\/?)$ /path/to/windows.php?type=$1&{QUERY_STRING} [NC]
You will have to change this according to your situation. I haven't tested this, but I hope it is good enough for you to get where you want.

How do i redirect a non-existent folder to similar folder?

I am creating a website where I want the members profile links as follows:
somedomain.com/someuser1/ -> somedomain.com/#!/home/someuser1/
somedomain.com/someuser2/ -> somedomain.com/#!/home/someuser2/
etc...
because i don't want to fill the root folder up with a bunch of member profile folders. Can this be done easily in the .htaccess file with rewrites?
I believe i would need to pull the 'someuser1' and 'someuser2' values and append them during the rewrite, but i'm not sure how to do that... the member folders will exist in the /home/ folder, but not the root folder...
Thanks!
Greg
Try this code in your .htaccess:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([^/]+)/?$ /#!/home/$1 [R=302,NE,L]
Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.
Will this work? Put this in the .htaccess file. Untested
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+) /#!/home/$1 [R=301,NC,L]

htaccess with specific folder exception

I've set up an htaccess file in order to create a small routing structure where urls redirect to index.php. I want to add an exception where if the user types in "/admin" and anything after, the url instead redirects to admin.php.
htaccess -
RewriteEngine On
RewriteBase /
RewriteRule ^admin/?(.*)$ admin.php?page=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
Example: http://mysite.com/admin/settings/ should lead to admin.php?page=settings, while http://mysite.com/about/ should lead to index.php (not using any parameters here). Regexp is really not my cup of tea, especially when there are multiple conditions to consider. Ideas?
Edit: ^admin/([^/]*)/? solved it.
You may try this in one .htaccess file in root directory:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !admin\.php [NC]
RewriteRule ^admin/([^/]*)/? /admin.php?page=$1 [NC,L]
RewriteCond %{REQUEST_URI} !/admin/ [NC]
RewriteRule .* /index.php [NC,L]
For permanent redirection, replace [NC],L] with [R=301,NC,L]

htaccess server side rewrite not working

I have my htacess rewrite working, the pages are going to where they are supposed to, but the url bar changes and I dont want it to. I thought this was an INTERNAL redirect and whatever is in the URL would be displayed. It's not working that way.
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^/[^/]+(/(.+))?$
RewriteRule . /%2 [L]
Basically, the url IS rewriting to the new URL. How do I get it to not do that?
Per your last comment, try this instead
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^/[^/]+(/?|/(.+))$
RewriteRule . /%2 [L]
It should match an optional trailing slash e.g. http://mydomain.com/somedir or http://mydomain.com/somedir/ and one with a directory after e.g http://mydomain.com/somedir/dir2
If whatever the %2 back reference is matching doesn't end with an extension, mod_dir might think that it's a directory. If it's missing a trailing slash, mod_dir will externally redirect the browser to the same URL but with a trailing slash. You could try turning DirectorySlash Off in your .htaccess file or server config.
edit
You can try to bypass mod_dir's by doing the directory check yourself and adding the trailing slash so mod_dir won't redirect you. It would look something like this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^/[^/]+(/(.+))$
RewriteCond %{DOCUMENT_ROOT}/%2 -d
RewriteRule ^[^/]+(/(.+))$ /$2/ [L]

Resources