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]
Related
I have this code in my htaccess file
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}\.php !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC]
This help to remove the .php extension in my url. but now I've been trying for weeks to rename a URL I have.
article.php?num=5 to article/num/5 or maybe the title of the article it self nothing has worked so far. this is what I tried using.
RewriteRule ^article/([0-9].+)/?$ article?num=$1 [NC]
Yet I always get internal server error i really need help with this its been frustrating.
Your RewritePattern doesnt accept the /num/ segment, you need to adjust it so it matches your your uri /article/num/digits . Change your pattern's regex to this
^article/num/([0-9]+)$
Try it like below rule,
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([\w-]+)$ $1.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w-]+)/num/([\d]+)$ $1.php?num=$1 [QSA,L]
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]
I am trying to find a better solution to solve the issue. I am not using any framework like silex, Yii, etc. Thus I do not have the general routing handling mechanism provided by these frameworks. My URL patterns are like
routing.php
routing.php?action=create
routing.php?action=list&id=1
and the resulting URL I want to have are
/routing
/routing/create
/routing/list/1
I have some very basic understanding of .htaccess, below is my current foundings
RewriteEngine On
RewriteBase /name/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
These codes work well to put any .php to /something. However, I am stuck to continue to generate a general solution for getting /routing/create and /routing/list/1 . I appreciate for any helps.
Have your .htaccess like this:
RewriteEngine On
RewriteBase /name/
RewriteCond %{DOCUMENT_ROOT}/name/$1.php -f
RewriteRule ^([^/]+)/([^/]+)/?$ $.php1?action=$2 [L,NC,QSA]
RewriteCond %{DOCUMENT_ROOT}/name/$1.php -f
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ $1.php?action=$2&id=$3 [L,NC,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
I hate to ask this question because it's been asked a million times, but the answers never seem satisfactory, and most of the threads seem abandoned without an accepted answer.
Here's exactly what I need to do (bad urls are intentional due to low karma):
http://example.com/file.php redirects to http://example.com/file/
http://example.com/file must also redirect to http://example.com/file/
http://example.com/asdfsadf and http://examplecom/file/asdfasdf must go to the 404 page
Here's the htaccess magic I cobbled together from posts here and elsewhere. It seems to work (unlike most of the abandoned threads on the topic, where there's always some strange behavior).
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]
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.#?\ ]+\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)\.php http://example.com/$1 [R=301,L]
Like I said, as far as I can tell, this works fine, even with subdirectories. Can any more knowledgeable folks tell me if I'm missing something. Could it be improved or shortened?
For what it's worth, I'm also removing the www:
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
It seems to work fine. It is included after the other parts. Is this the best order?
Thanks everyone, I hope we can get a good, reliable answer for this out there because there's a lot of bad ones.
Oh, I have the answer to this one!
This little rewrite snippet to go in .htaccess will remove the extension from any file you specify in its url.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteBase /
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://yourdomain.com/$1/ [L,R=301]
Just paste it at the bottom of your .htaccess file in your root directory. The file is hidden so make sure 'show hidden files' is enabled in your ftp. ETA: last 3 lines should add a trailing slash to all files at yourcomain.com Do't forget to replace youdomain.com correctly.
That will remove the '.php' from all your urls to php files! ^_^
Hope I helped
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]