[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 384
  • Last Modified:

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

0
igotdreams
Asked:
igotdreams
  • 2
1 Solution
 
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
 
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now