I’m new to the rewriting of urls and regex in general. I’m trying to rewrite a URL to make it a ‘pretty url’
The original URL was
/localhost/house/category.php?cat=lounge&page=1
I want the new url to look like this:
/localhost/house/category?lounge&page=1
(like I say, I’m new so not trying to take it too far at the moment)
the closest I’ve managed to get it to is this:
RewriteRule ^category/(.*)$ ./category.php?cat=$1 [NC,L]
but that copies the whole URL and creates:
/localhost/house/category/house/category/lounge&page=1
I’m sure, there must be an easy way to say copy all after that expression, but I haven’t managed to get there yet.
2
Answers
Thanks for all your suggestions, I took it back to this
RewriteRule category/([^/])/([0-9])/?$ category.php?cat=$1&page=$2 [NC,L]
which has done the trick, and I'll leave it at this for now.
I will try to help you:
For your solution, try:
This will insert the
.php
andcat=
while retaining the&page=
Anticipating your next step, the below mod rewrite may help get started in converting
to
Only RewriteRule necessary here, no query string:
Use regex101 for more help and detailed description on what these regexes do.
If it still not working, continue to make the regex more lenient until it matches correctly:
Try to remove the ^ in RewriteRule so it becomes
Then it will match that page at any directory level. Then add back in
house/
and add/?
wherever an optional leading/trailing slash may cause a problem, etc.