.htaccess - Clean URL, 500 Internal Server Error - .htaccess

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]

Related

How can I have 2 rewrite rules on my .htaccess file

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.

htaccess remove param key not working as intended

I currently have in place a rewrite rule to remove a key on one of the sites.
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*)$ /i/index.php?id=$1 [NC,L]
I now want to add this rule.
RewriteRule ^(.*)$ /stash/index.php?sid=$1 [NC,L]
However, this does not work.
HOST_NAME/stash/123 will not work.
HOST_NAME/stash?sid=123 will.
The PHP is simple....
echo $_GET['sid'];
Here is my full HTACCESS
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(www\.)?hostname\.com
RewriteRule ^(.*)$ https://www.hostname.com/$1 [R,L]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*)$ /i/index.php?id=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*)$ /stash/index.php?sid=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /results/index.php?/$1 [L]
Any help on this would be great.
UPDATE
There are four main directories on the website.
/results
/i
/stash
/account
I dont care if /results shows ?id=1$store=1...etc
I dont want /i and /stash to show that. I would want those to look like...
/i/123456789 instead of /i?id=123456789
/stash/hgdG54Hj9 instead of /stash?sid=hgdG54Hj9
Looks like you have nice directory based URI structures. In that case you can just have a per directory .htaccess
You may use this code in your root .htaccess:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(?:www\.)?(hostname\.com)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
Inside stash/.htaccess:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?id=$1 [L,QSA]
And similarly inside i/.htaccess:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?id=$1 [L,QSA]

Rewrite rules for removing the `.html` file name extension

I have tried following command in the the .htaccess file to remove .html extension and put slash at the end. But it is not working. Can anyone suggest me what is going wrong?
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.html
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
Use these rules in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+(.+?)\.html[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^(.+?)/?$ /$1.html [L]

multiple rules of URL rewriting

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]

.htaccess rewriterule not working for single page

I have a couple pages:
http://waleyqiao.com/about/
http://waleyqiao.com/index/
http://waleyqiao.com/contact/
http://waleyqiao.com/blog/
http://waleyqiao.com/portfolio/
The real pages would be waleyqiao.com/about.html
htaccess:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^/]+)/$ $1.html
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
For some reason, waleyqiao.com/portfolio/ does not seem to work but waleyqiao.com/portfolio.html does...
It's because you are tacking the .html right on the end of the URI:
/portfolio/.html
which obviously doesn't exist. You need to create a grouping without the trailing slash. Try this instead:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(.*?)/?$
RewriteCond %{DOCUMENT_ROOT}/%1\.html -f
RewriteRule ^ /%1.html
And likewise for the php extension:
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(.*?)/?$
RewriteCond %{DOCUMENT_ROOT}/%1\.php -f
RewriteRule (.*) /%1.php [L]

Resources