I currently have an .htaccess file that has one rewrite rule that removes .html however I cannot seem to be able to do the same thing with .php without messing up my whole website
This is the code that I have in my .htaccess file.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [NC,L]
Thanks
Sure you can, why not?
Your version:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
Some lightly modified version:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^ %{REQUEST_URI}.html [END]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^ %{REQUEST_URI}.php [END]
You generally should switch off MultiViews when implementing such rules by the way.
Related
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html[L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9_-]+)$ /profile.php?name=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ /profile.php?name=$1
This is currently .htaccess of my website. The rewriting supposes to have two functions, makes the internal url example/profile.php/name=xyz into example/xyz as external url, and also removes the extensions of html files.
But the extension removing is not working, and such as for a file named xyz.html, it gets 404 and
example.com/xyz/?name=xyz
At the address bar. I think there is some conflicts between two rules.
RewriteCond is only applicable to very next RewriteRule hence your last rule is running without any RewriteCond. Also you need to use [L] (Last) flag in all of your rules.
Use this code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ /$1.html [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ /$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w-]+)/?$ /profile.php?name=$1 [L]
Currently I drop the .php extension in the URL.
I use:
RewriteEngine on
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]
The problem is that if I have a folder it doesn't work. I have to add it to every folder group. I know there is an easier way.
Can anyone help?
You just need to change the regex so that you aren't excluding requests that have a / in it.
Change your first rule to:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+)/$ $1.php
I've got this as my .htaccess:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
# Where it's all about
RewriteRule ^users/([a-zA-Z0-9]+)$ users.php?user=$1
RewriteRule ^users/([a-zA-Z0-9]+)/$ users.php?user=$1
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.daltonempire.nl [nocase]
RewriteRule ^(.*)
http://daltonempire.nl/$1 [last,redirect=301]
I tried to make clean urls, redirecting daltonempire.nl/users/Me to daltonempire.nl/users.php?user=Me.
However, I failed miserably. My website now constantly returns a 500 Internal Server Error.
(I possibly somehow created a redirection loop?)
What did I do wrong? (And what do I have to do to fix it?)
Believe you have an extra newline in last rule. Also make sure to use L (Last) flag in all of your rules.
RewriteEngine on
# Where it's all about
RewriteRule ^users/([a-zA-Z0-9]+)/?$ users.php?user=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.+?)/?$ $1.html [L]
RewriteCond %{HTTP_HOST} ^www\.(daltonempire\.nl)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [L,R=301,NE]
this snippet works fine for removing php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
However, if I add html condition, it will not work
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
Did I do something wrong?
You may try this instead, in one .htaccess file at root directory:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !\.php [NC]
RewriteRule .* %{REQUEST_URI}.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteCond %{REQUEST_URI} !\.html [NC]
RewriteRule .* %{REQUEST_URI}.html [NC,L]
You should be aware the implementation in your code works only when the request doesn't have a trailing slash. I just modified your code to work, but I think it's worth nothing that problem. For example:
For a request like http://example.com/filename/, the file to match with condition RewriteCond %{REQUEST_FILENAME}.php -f is filename/.php. That file can't exist, therefore, the respective rule will be never used.
Option to remove the trailing slash when present:
Add these lines after RewriteEngine On in the previous code:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /(.*[^/]+)/$ [NC]
RewriteRule .* /%1 [NC,L]
How can I rewrite
www.example.com/test.php?search=demo
to
www.example.com/test/?search=demo
using htaccess
Put that into your .htaccess:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([A-Za-z0-9]+)/?$ $1.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php