Home > Mobile >  How to I replace a part of the URL string containing a query using htaccess?
How to I replace a part of the URL string containing a query using htaccess?

Time:12-28

I have a URL string using a PHP query string. I want to make the URL pretty, but I haven't been able to make the rule properly.

Currently the URL looks like this:

http://localhost/pages/map?name=Skyfall-2022

But I want it to look like this:

http://localhost/pages/Skyfall-2022

This is my current htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.] )$ $1.php [NC,L]
RewriteRule ^([^\.] )$ $1.html [NC,L]


RewriteRule ^pages/(. )$ /pages/map?name=$1

I get a 500 Internal Error with this when I try to type in the desired URL. I don't really understand Regular Expressions so I would appreciate if someone could adjust this for me with an updated .htacces

CodePudding user response:

With your shown samples, please try following htaccess rules.

Please make sure to clear your browser cache before testing your URLs.

RewriteEngine ON
##External redirect for url change in browser rules here...
RewriteCond %{THE_REQUEST} \s/(pages)/map/?\?name=(\S )\s [NC]
RewriteRule ^  %1/%2? [R=301,L]

##Internal rewrite here for internal file's serving here.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/(.*)/?$  $1/all-maps.php?name=$2 [QSA,L]

NOTE: I have mentioned map? in rewrite rules(2nd set of rules written in comments also in rules), you can change it with your php file's name whatever php file you have to pass query string to.

  • Related