1

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?

OneNation
  • 427
  • 1
  • 8
  • 22
  • 1
    What does the error log have to say about this? – CBroe Nov 02 '21 at 14:39
  • And where is your .htaccess located? – CBroe Nov 02 '21 at 14:39
  • Error log says [pid 4448] [client ::1:55666] AH00124: Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace. – OneNation Nov 02 '21 at 14:41
  • It is located at localhost/site/.htaccess – OneNation Nov 02 '21 at 14:41
  • Probably because only made an internal rewrite, not an external redirect. Add the `R` flag to your rule (for testing, `R=302` is recommended, you can change it to `301` later on when everything works.) – CBroe Nov 02 '21 at 14:43
  • What other directives do you have in your `.htaccess` file? This alone won't cause the rewrite-loop (Internal Server Error) you describe (although it's not correct either). You would need other directives for this to work. Where is this `.htaccess` file located? As written, it is not interchangeable between a subdirectory and the document root. – MrWhite Nov 02 '21 at 16:58
  • thats all i have. i am new to this .htaccess – OneNation Nov 02 '21 at 21:26
  • Edited : I managed to get it to work a bit, but I am still stuck with /place/area. – OneNation Nov 03 '21 at 02:23

1 Answers1

1

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.

Amit Verma
  • 40,709
  • 21
  • 93
  • 115
  • OMG. This is almost right. However, I am unable to get this part to work. – OneNation Nov 03 '21 at 11:50
  • this part: From https://localhost/site/?page=place&place=west to https://localhost/site/place/west – OneNation Nov 03 '21 at 11:51
  • 1
    @OneNation Glad it was helpful. For the second part to work, you will need to write a new rule that can accept query strings with two parameters `page` and `place` . The rule above only rewrites URLs with one query perm `page` . If you need help with that please post a new question. – Amit Verma Nov 03 '21 at 13:36
  • okay i have created new question https://stackoverflow.com/questions/69827605/url-rewrite-multiple-query – OneNation Nov 03 '21 at 15:34