Add trailing-slash in URL structure - .htaccess

Can you please help me to write correctly .htaccess file. I have this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
So it rewrites from domanname.com/page.php to domanname.com/page - but if I type in domanname.com/page/ it goes to 404 page. What should I change to correct this problem.
Thanks.

Adding a trailing slash at the end
I received many requests asking how to add a trailing slash at the end. Ignore the first snippet and insert the following code. The first four lines deal with the removal of the extension and the following, with the addition of the trailing slash and redirecting. Link to the HTML or PHP file as shown above. Don’t forget to change the code if you want it applied to an HTML file.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
More info: http://alexcican.com/post/how-to-remove-php-html-htm-extensions-with-htaccess/

If you want to match a pattern whether or not there's a trailing slash, add the trailing slash followed by the regex optional operator ?.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)/?$ $1.php [NC,L]
Also, added a RewriteCond to prevent existing directories from matching.

You can use this rule to allow for an optional trailing slash:
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]

Related

.htaccess "Internal Server Error" when adding / to URL

I've got the following .htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^profile/([A-Za-z]+) profile.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
But you can't add a / to the end of the URL. For example if you goto something it works but something/ doesn't. So how do I allow a / to be added to the URL?
Here is a possible solution if anyone else is having the same problem (not fully tested but seems to work).
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^profile/([A-Za-z]+) profile.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*?)(/.*)?$ $1.php [NC,L]
To allow the trailing slash in URLs that point to your php files, replace your last rule with this :
RewriteRule ^(.*)/?$ $1.php [NC,L]
/? matches the slash as an optional character so your rule will also match URIs not ending with a / .

rewrite error when one value is empty

I have this line here;
RewriteRule ^news/(.*)/(.*)/$ ./news.php?type=$1&number=$2 [L]
But when one of the 2 values is empty it shows an error that the page is not found. As example I did;
localhost/news/dgfgh
Is there a way to fix this?
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} "/admin/"
# Remove .php extension
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ /$1/ [L,R=301]
RewriteRule ^news/(.*)/(.*)/$ ./news.php?type=$1&number=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(.+)/$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^(.*)/$ $1.php [L]
# Force trailing slash
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .*[^/]$ $0/ [L,R=301]
The Regular Expression in your rewrite rule does not match localhost/news/dgfgh
The rewrite rule is looking for news followed by exactly two groups, followed by a trailing slash. To do what you want, you need two rules.
RewriteRule ^news/(.*)/(.*)/?$ ./news.php?type=$1&number=$2 [L]
RewriteRule ^news/(.*)/?$ ./news.php?type=$1 [L]
The first one is yours with a simple ? before the trailing slash to indicate that the trailing slash is options. The second one is for the case when you don't have the number in the url as in your example

Removing the extension won't work

I've searched a whole lot to get this done.
I've found many solutions which people have claimed to be working.
But it's just not working for me. And I've got no idea why that is.
Below is my .htaccess as it is right now.
The first 4 lines of code are working correctly, the rest isn't.
Also, do I need to change the links in my navigation aswell?
For the website click here.
CODE
Options Includes FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !^dylanvanheugten.nl$ [NC]
RewriteRule ^(.*)$ http://dylanvanheugten.nl/$1 [R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php
# Forces a trailing slash to be added
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
Can anyone please help me fix my file?
Thanks in advance!
And one last thing, please explain what you are doing. ;-)
This condition:
RewriteCond %{REQUEST_FILENAME}\.php -f
will fail because you have a trailing slash. For example, if you request /foo/bar/ expecting to get served the contents at /foo/bar.php, the trailing slash would make this condition check that /foo/bar/.php exists as a file, which doesn't.
You need to tailor your check to ignore the trailing slash:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/([^/]+)/$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^([^/]+)/$ $1.php
The other thing you are missing is an external redirect for requests made for php files:
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /([^/]+)\.php
RewriteRule ^ /%2/ [L,R=301]

htaccess - URL rewrite - Remove slashes, but not from files

I use PHP.
A working htaccess-file
RewriteEngine On
RewriteBase /folder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule .*[^/]$ $0/ [L,R=301]
RewriteCond $1 !^(index\.php)
RewriteRule ^(.+)$ index.php/$1 [L]
Look at the row with a # comment. When uncommented it adds a redirect to a slash. I use URL rewrite with toroPHP.
I want to rewrite to ending slash
I want to redirect to ending slash from rewritten URLs, just like the code above.
I don't want ending slash from real files, like jquery.js, style.css.
Example (updated 2012-12-21)
/category/test should be /category/test/
http://www.test.com/myjsfile.js should be http://www.test.com/myjsfile.js
Problem
If I use the code above uncommented it add an ending slash to all urls, including javascript files and css files.
I only want the rewritten urls to end with slash.
Question
Can it be done with htaccess? If so how?
The htaccess was almost correct. However the REQUEST_FILENAME needs to be before EVERY rewrite rule, not just before the first.
This works
RewriteEngine On
RewriteBase /folder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .*[^/]$ $0/ [L,R=301]
RewriteCond $1 !^(index\.php)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php/$1 [L]

htaccess code to remove extension AND add+force trailing slash?

I'm trying to put together some htaccess code that will turn example.com/filename.php into example.com/filename/ (and force the slash) - I've tried varous approaches, but each hasn't worked quite right, from 500 errors on subfolders to issues with the trailing slash, etc...
Please help!
Try this:
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ /$1/ [L,R=301]
RewriteRule (.*)/$ $1.php [L]
The first rule redirects requests of /foo/bar.php externally to /foo/bar/. And the second rule rewrites requests of /foo/bar/ internally to /foo/bar.php.
And to force the trailing slash, try this rule:
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .*[^/]$ $0/ [L,R=301]
This solution of Gumbo works great for files, yet it does not work with directories.
In other words:
For mysite.com/file1.php, it shows mysite.com/file1/ which is great.
Yet, it does not work well for directories. If I try to access the following directory (that contains index.php file inside) mysite.com/dir1, instead of showing the content of the http:/mysite.com/dir1/index.php and the url: mysite.com/dir1/, it returns 404.
My solution around it was:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ /$1/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)/$ $1.php [L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .*[^/]$ $0/ [L,R=301]
In other words, not to do anything if its a directory.
Hope it helps.
Another problem with the original solution is that css and the images are not loaded until I change the path to the css file and to images to absolute path.
Is there any other way to solve it, rather then changing all the paths in all the files in the website to absolute.
Thanks a lot.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
Works like a charm - thanks for the help folks.
You could try working off the question here. Whilst the solution to the question isn't relevant (yet...) the question itself provides a set of rewrite rules which you may be able to use in your own site.
If you require symbols in URLs you could just use ".*" instead of the specific A-Za-z0-9, but if you're looking for a possible trailing slash you may want to use ".*?" instead. This is a standard regular expression feature to avoid the greediness of ".*".
For the path you can add <base href="yourpath" /> to your php pages.
Try this, this one works on my Apache, even when you remove manual the last slash:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ /$1/ [L,R=301]
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]

Resources