0

How to rewrite this URL:

https://example.com/illustrations.php?category=cats&cat_id=1 

to:

https://example.com/category/cats 

also, how do I still preserve cat_id param?

I tried this but it does not work:

RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^/?category/(.*?)/?$ /illustrations.php?category=$1 [L] 
6
  • "it does not work" - What happens exactly? Your directive looks "OK". "still preserve cat_id param" - what do you mean exactly? Given the URL /category/cats then the only thing you can do is to seemingly hard-code the cat_id parameter in the substitution string. Otherwise, you need to include the cat_id in the URL. "How to rewrite this URL..." - it's the other way round, you are rewriting that URL to this URL (which is what you are doing in your .htaccess rule). Commented Jul 21, 2020 at 0:16
  • If you want to preserve the cat_id parameter it needs to be part of the new URL pattern too. So maybe https://example.com/category/cats/1. Without that there is no way to implement a working redirection since the rewriting module cannot somehow make up such ids by magic. Commented Jul 21, 2020 at 7:02
  • @MrWhite When I add this code and go to illustrations.php?category=cats it does not change the url in the browserbar. Commented Jul 21, 2020 at 10:02
  • @arkascha and how would I achieve this? Commented Jul 21, 2020 at 10:03
  • What URL are you linking to in your application/site? Commented Jul 21, 2020 at 15:01

1 Answer 1

1

When I add this code and go to illustrations.php?category=cats it does not change the URL in the browserbar.

Yes, that is "correct".

The code you posted internally rewrites the URL /category/cats (which is the URL you should be linking to in your HTML source) back to the actual filesystem/URL path: /illustrations.php?category=cats. This is required in order to make the "pretty" URL /category/cats "work".

You can't change the URL structure using .htaccess only - if that is what you are implying. You do need to actually change the physical URLs in your HTML source.

You could implement an external redirect (not a rewrite) from /illustrations.php?category=cats to the canonical URL /category/cats using .htaccess, but note that this is only to benefit SEO (and third parties that might have already linked to the old URLs). This is a necessary additional step if you are changing an existing URL structure and SEO is a concern, but it is not part of the "working" of your site.

how do I still preserve cat_id param

You would need to include the value of the cat_id parameter in the URL. eg. /category/cats/1 (as @arkascha suggested in comments) or /category/1/cats - depending on which value is more important. I would put the more important value first, since URLs can be accidentally cropped when shared.

RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^/?category/(.*?)/?$ /illustrations.php?category=$1 [L] 

This rule could be simplified. Filesystem checks are relatively expensive. It looks like you could remove both of these by making your regex more specific. eg. Could /category/1/cats ever map to a file? Do you need directories to be accessible? I would expect the answer to both those questions is "no".

I would also decide on whether to allow trailing slashes or not, rather than allowing both (as in your current rule). Strictly speaking this creates duplicate content (two URLs; same content), so requires additional steps to resolve. Your example URL(s) do not contain a trailing slash.

So, you could simplify your rules to the following in order to rewrite /category/1/cats (no trailing slash) to /illustrations.php?category=cats&cat_id=1

RewriteEngine On RewriteRule ^category/(\d+)/([\w-]+)$ /illustrations.php?category=$2&cat_id=$1 [L] 

This assumes cat_id can be 1 or more digits (0-9). And category is limited to the following characters: a-z, A-Z, 0-9, _ (underscore), - (hyphen).

With regex it is preferable to be as restrictive as possible. By omitting the dot (.) from the last path segment in the above rule it cannot match a physical file (assuming all your files have file extensions).

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot for the explanation. I'm new to working with htaccess. So I think I did not understand the concept of htaccess. I would like to thank you for explaining it so nicely.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.