A
A
Artur Kosikov2017-09-17 07:01:27
URL Handling
Artur Kosikov, 2017-09-17 07:01:27

How to register with modrewrite rule?

Faced the problem of implementing the condition for modrewrite.
Task: If the first element of the URL is the name of a city from the list of cities, redirect a request like:

/city_name/category/subcategory/...etc

on the
/category/subcategory/...etc/?city=city_name

It works well if you write this code in .htaccess
RewriteCond %{REQUEST_URI} ^/(bucuresti|verona|sofia)/
RewriteRule ^([a-z0-9-]+)/(.+)$ /$2?region=$1 [R,L]

However, the list of cities is quite unwieldy (more than 3k), so I decided to go the other way...
I want to redirect ALL requests, except for those that are not cities (the list of those is much shorter)... so I tried to write like this :
RewriteCond %{REQUEST_URI} !^/(help|contacts|about)/
RewriteRule ^([a-z0-9-]+)/(.+)$ /$2?region=$1 [R,L]

(which, in my opinion, should have worked the same way, because everything that I changed - put NOT (sign !) in front of the list) ...
But it doesn’t want to work “as it should” :(
For some reason, it “sees” only the last two parameter, for example /verona/cat/subcat redirects to /subcat?region=cat , which puts me in a stupor... Who can tell me?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artur Kosikov, 2017-09-17
@atillus

I figured it out... More precisely, they suggested it :)
After the redirect, the new address starts with /cat/ and again matches the condition !^/(help|contacts|about)/
You can optionally check %{QUERY_STRING} for region.

RewriteCond %{QUERY_STRING} !region=

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question