Home > Back-end >  Rewrite Rule keep showing Internal Server Error
Rewrite Rule keep showing Internal Server Error

Time:11-06

I am trying rewrite rule and has been stuck for so long.

RewriteEngine on
RewriteCond %{QUERY_STRING} ^page\=about$
RewriteRule ^$ /about? [L]

What I am trying to achieve is:

From

https://localhost/site/
https://localhost/site/?page=about
https://localhost/site/?page=food
https://localhost/site/?page=place
https://localhost/site/?page=shop
https://localhost/site/?page=place&area=west

To

https://localhost/site/
https://localhost/site/about
https://localhost/site/food
https://localhost/site/place
https://localhost/site/shop
https://localhost/site/place/west

Whenever I try on my localhost or my domain (top level domain: www.mydomain.com), I keep getting : Internal Server Error


Edit: Now I have the following code. There's no error.

RewriteEngine on
#1) redirect the client from "/index.php/foo/bar" to "/foo/bar"
RewriteCond %{THE_REQUEST} /index\.php/(. )\sHTTP [NC]
RewriteRule ^ /%1 [NE,L,R]
#2)internally map "/foo/bar" to "/index.php/foo/bar"
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(. )$ /index.php?page=/$1 [L]

No error but if I were to go to https://localhost/site/about It will redirect me to homepage content even though the URL is https://localhost/site/about

But on live site, www.mysite.com, it works almost perfectly.

www.mysite.com/about √ works
www.mysite.com/shop √ works
www.mysite.com/place/west x not working

Since I copy this from another source, the comment said that #2)internally map "/foo/bar" to "/index.php/foo/bar".

-How do I make this map to index.php?page=about?

-How do I make it work for www.mysite.com/place/west?

CodePudding user response:

You can use the following rule :

 RewriteEngine On
 #Redirect /site/?page=foobar to /site/foobar
RewriteCond %{THE_REQUEST} /site/(?:index\.php)?\?page=(. )\sHTTP [NC]
RewriteRule ^ /site/%1? [L,R]
# Internally rewrite new path to the original one
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?:site/)?(. )/?$ /site/?page=$1 [L,QSA]

I haven't tested it but I believe it should work for your URLs.

  • Related