Mod Rewrite Condition for Unknow Sub Folder Name

I am working on a mod rewrite below;

I am bascially creating folders for each US state in a folder called /en/

which will look like this;

www.domain.com/en/Texas/

And then dynamically displaying the city and company name based on the data that is passed in the address. Like this;

www.domain.com/en/Texas/index.php?city=Dallas&name=AAA-Plumbers

I have created a site that had a mod rewrite to pass the states as well, but I have decided to create folder on the server for each state instead.

So now I need to adapt my mod rewrite code for an unknown state. See the code below, I need a wildcard variable to something for state where I have place /UNKNOWN/ in the code below.
<IfModule mod_rewrite.c>
ErrorDocument 404 /errors/404.php
Options +FollowSymLinks
RewriteEngine On
 
RewriteCond %{ENV:REDIRECT_STATUS} =""
RewriteCond %{QUERY_STRING} ^city=([^&]+)$
RewriteRule ^en/UNKNOWN/index\.php$ /en/UNKNOWN/%1? [R=301,L]
RewriteCond %{ENV:REDIRECT_STATUS} =""
RewriteCond %{QUERY_STRING} ^city=([^&]+)&name=([^&]+)$
RewriteRule ^en/UNKNOWN/index\.php$ /en/UNKNOWN/%1/%2? [R=301,L]
 
RewriteRule ^en/UNKNOWN/([^/]+)/?$ /en/UNKNOWN/index.php?city=$1 [L]
RewriteRule ^en/UNKNOWN/([^/]+)/([^/]+)/?$ /en/UNKNOWN/index.php?city=$1&name=$2 [L]
</IfModule>

Open in new window

igotdreamsAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

caterham_wwwCommented:
change UNKNOWN into ([^/]+) and adjust the backrefrence $n in the substitution:
ErrorDocument 404 /errors/404.php
Options +FollowSymLinks
RewriteEngine On
 
RewriteCond %{ENV:REDIRECT_STATUS} =""
RewriteCond %{QUERY_STRING} ^city=([^&]+)$
RewriteRule ^en/([^/]+)/index\.php$ /en/$1/%1? [R=301,L]
RewriteCond %{ENV:REDIRECT_STATUS} =""
RewriteCond %{QUERY_STRING} ^city=([^&]+)&name=([^&]+)$
RewriteRule ^en/([^/]+)/index\.php$ /en/$1/%1/%2? [R=301,L]
 
RewriteRule ^en/([^/]+)/([^/]+)/?$ /en/$1/index.php?city=$2 [L]
RewriteRule ^en/([^/]+)/([^/]+)/([^/]+)/?$ /en/$1/index.php?city=$2&name=$3 [L]

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
igotdreamsAuthor Commented:
With some adjustments to your code, I was able to get on the right path. The code that works is below.

For anyone that comes int he future, the code allows me to do this;

domain.com/en/Texas/Dallas/
or
domain.com/en/Texas/Dallas/CompanyName

and receive it with my hardcoded page;

domain.com/en/Texas/index.php?city=Dallas&name=CompanyName

Then I GET the variables with PHP and query the database, if the database is empty I will throw a 404 error page.
<IfModule mod_rewrite.c>
ErrorDocument 404 /errors/404.php
Options +FollowSymLinks
RewriteEngine On
 
RewriteCond %{ENV:REDIRECT_STATUS} =""
RewriteCond %{QUERY_STRING} ^city=([^&]+)$
RewriteRule ^en/([^/]+)\index\.php$ /en/$1/%1? [R=301,L]
 
RewriteCond %{ENV:REDIRECT_STATUS} =""
RewriteCond %{QUERY_STRING} ^city=([^&]+)&name=([^&]+)$
RewriteRule ^en/([^/]+)\index\.php$ /en/$1/%1/%2? [R=301,L]
 
RewriteRule ^en/([^/]+)/([^/.]+)/?$ /en/$1/index.php?city=$2 [L]
RewriteRule ^en/([^/]+)/([^/]+)/([^/]+)/?$ /en/$1/index.php?city=$2&name=$3 [L]
</IfModule>

Open in new window

0
igotdreamsAuthor Commented:
Thank you so much for your direction.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Development

From novice to tech pro — start learning today.