3

I am trying to see how I can achieve the following rewrite rules.

From

https://localhost/site/?page=place&place=west
https://localhost/site/?page=location&location=cityname

To

https://localhost/site/place/west
https://localhost/site/location/city

I am able to change https://localhost/site/?page=place to https://localhost/site/place but not with another additional query as mentioned above.

htaccess

 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]

The above htaccess works for the following which is what I also need.

https://localhost/site/place
https://localhost/site/about
https://localhost/site/contact
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
OneNation
  • 427
  • 1
  • 8
  • 22

1 Answers1

4

With your shown attempts, please try following htaccess rules file. Make sure to clear your browser cache before testing your URLs. New rules are clubbed to your already existing rules.

RewriteEngine ON
RewriteBase /site/
##New rules from here......
RewriteCond %{THE_REQUEST} \s/site/?\?page=([^&]*)&place=([^&]*)\s [NC]
RewriteRule ^ /site/%1/%2? [R=301,L]

# Internally rewrite new path to the original one
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^site/([^/]*)/(.*)/?$ index.php?page=$1&location=$2 [NC,QSA,L]
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
  • 1
    Shouldn't `&place=` also follow the `([^&]*)` rule as there's no guarentee that this will be the final query string argument in the future – Martin Nov 03 '21 at 17:14
  • @Martin, that could be also used, but I have used all non-spaces capturing, because if a space comes that will end of THE_REQUEST variable's main part(which e need uri one). – RavinderSingh13 Nov 03 '21 at 17:15
  • 3
    But using "\S+" on `https://localhost/site/?page=place&place=west&home=yes` would mean %2 = "west&home=yes" ? – Martin Nov 03 '21 at 17:27
  • 1
    @Martin, yeah sure, I have edited my solution as per suggestion now. Thank you. – RavinderSingh13 Nov 03 '21 at 17:38
  • Thanks! I think it is semi-working.. The URL is display as https://localhost/site/place/west which is perfect but it is unable to display content. I am receiving this error. Warning: include_once(page/place/west.php): failed to open stream: No such file or directory in [filepath] – OneNation Nov 03 '21 at 22:55
  • @OneNation. Ok, so link https://localhost/site/place/west should be served by which file? Eg: index.php index.html etc? – RavinderSingh13 Nov 04 '21 at 02:07
  • should be served by index.php @RavinderSingh13 – OneNation Nov 04 '21 at 05:01
  • @OneNation, oh ok sure and do you want to send some parameters to index.php also? Like: `index.php?abc=def` etc? – RavinderSingh13 Nov 04 '21 at 05:02
  • @RavinderSingh13 Yes, Some of the parameters would be index.php?page=place&place=west and index.php?page=location&location=cityname – OneNation Nov 05 '21 at 00:01
  • 1
    @OneNation, sure, could you please do check my edited code once and let me know if this helps you? Thank you. – RavinderSingh13 Nov 05 '21 at 15:11
  • 1
    @RavinderSingh13 it didn't work :( I tried location/cityname - the include_once(page/location/cityname.php) failed to open stream: No such file. – OneNation Nov 05 '21 at 16:14
  • Let me try to give more information.. maybe it will help.. index.php?page=location --> gets data from page/location.php. Then index.php?page=location&location=cityname --> gets data from the file of page/location.php , in location.php $_GET['location'] is used to get the cityname value. So with index.php?page=location&location=cityname, the $_GET['page'] is used to retrieve which file to use in 'page/' folder. The $_GET['location'] is used in location.php to query from DB where cityname=$_GET['location'] @RavinderSingh13 – OneNation Nov 05 '21 at 16:27
  • 1
    If URL is displayed as `https://localhost/site/one/two` then how it should be handled internally? It cannot be `index.php?page=one&location=two` and `index.php?page=one&place=two` at the same time. – anubhava Nov 08 '21 at 06:11