2

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.

Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
T Carter
  • 41
  • 4

2 Answers2

1

I will try to help you:

  1. You probably have already, but try a mod rewrite generator and htaccess tester.
  2. From this answer: The query (everything after the ?) is not part of the URL path and cannot be passed through or processed by RewriteRule directive without using [QSA].
  3. I propose using RewriteCond and using %1 instead of $1 for query string matches as opposed to doing it all in RewriteRule.

For your solution, try:

RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^house/category$ house/category.php?cat=%1 [NC,L]

This will insert the .php and cat= while retaining the &page=


Anticipating your next step, the below mod rewrite may help get started in converting

http://localhost/house/category/lounge/1

to

http://localhost/house/category.php?cat=lounge&page=1

Only RewriteRule necessary here, no query string:

RewriteRule ^house/category/([^/]*)/([0-9]*)/?$ house/category.php?cat=$1&page=$2 [NC,L]

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

RewriteRule category$ category.php?cat=%1 [NC,L]

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.

TheWandererLee
  • 1,012
  • 5
  • 14
  • I had tried a generator, but that hadn't helped. Thanks for the link to the tester, I hadn't found one as good as that one. The solution you've given isn't working unfortunately. on the site it's having no effect, and the tester page replies to say that the rule was not met. – T Carter May 13 '19 at 15:48
  • Sorry, I had to modify the URL domain to get it to work for that tester. It is expecting localhost or it will change your input domain to :80. Add house/ to RewriteRule as I have edited to add in my answer. – TheWandererLee May 13 '19 at 16:25
  • 1
    Thanks, you've predicted the next step well, although I was going to delay before going that far, due to realising that I need to try and take a course before attempting too much more!! That looks like it should work in the tester, but for some reason still isn't on the actual site. I will have a play around later, after the kids are in bed, to see what is going wrong. Thank you for your help. the tester is changing the & to %26, will the URL still recognise this as an ampersand, will it recognise the unicode? or is that just a symptom of the tester – T Carter May 13 '19 at 16:37
  • The & being replaced by %26 is a PHP symptom of the tester I believe, other answers indicate the & character is fine to use on mod rewrite. Try my last step to make the match more lenient, play around with it, eventually you will get it to match even if you must do match .* to reroute everything! Then slowly walk back adding more until you see what the URL is doing. Also turning on the rewrite log might help you a lot: https://stackoverflow.com/a/9632952/5514077 – TheWandererLee May 13 '19 at 16:51
  • thank you, I took it back to: RewriteRule category/([^/]*)/([0-9]*)/?$ category.php?cat=$1&page=$2 [NC,L] which has done the trick, Thank you for all your help – T Carter May 13 '19 at 20:52
1

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.

T Carter
  • 41
  • 4