charlie
charlie

Reputation: 1384

htaccess Mod Rewrite rule for multiple URLs and PHP $_GET's

I have these mod rewrite rules in my htaccess file:

RewriteEngine On

# Displays directory if there is no / on the end of the URL
RewriteCond %{REQUEST_URI} !^/admin [NC]
RewriteCond %{REQUEST_URI} !^/status [NC]
RewriteCond %{REQUEST_URI} !^/customer [NC]

# Removes index.php from URL 
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php$ /$1 [L,R=302,NC,NE]

# Rewrites /services to be /index.php?id=services
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-]+)/?$ /index.php?id=$1 [L,QSA]

# Rewrites /blog/this-is-a-blog-post to be /index.php?id=blog&slug=this-is-a-blog-post
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-]+)/([\w-]+)/?$ /index.php?id=$1&slug=$2 [L,QSA]

# Rewrites /blog/year2013/month12 to be /index.php?id=blog&year=2013&month=01
RewriteRule ^([a-zA-Z0-9-]+)/year([0-9]+)/month([0-9]+)/?$ /index.php?id=$1&year=$2&month=$3 [L,QSA]

# Rewrites /status/123 to be /index.php?id=status&seq=123
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-]+)/([\w-]+)/?$ /index.php?id=$1&seq=$2 [L,QSA]

# Rewrites /status/cat1 to be /index.php?id=status&cat=1
RewriteRule ^([a-zA-Z0-9-]+)/cat([0-9]+)?$ /index.php?id=$1&cat=$2 [L,QSA]

the /blog works fine, but /status doesn't. its showing the directory index of even tho there is no directory

i basically want:

  1. home.php?id=services to look like domain.com/services (this for multiple links/URLs) - Works fine
  2. home.php?id=blog&year=2013&month=12 to look like domain.com/blog/year2013/month12 Works fine
  3. home.php?id=blog&slug=this-is-a-blog-post to look like domain.com/blog/this-is-a-blog-post Works fine
  4. home.php?id=status&cat=123 to be domain.com/status/cat123 Not working
  5. home.php?id=blog&seq=456 to be domain.com/status/456 Not working

as you can see above only numbers 4 and 5 don't work, they are just showing either index of or 404 page not found however there is no directory stored on the web server with a name of status

how can I fix my code above to get this working as the list above?

Upvotes: 2

Views: 2266

Answers (1)

anubhava
anubhava

Reputation: 786289

You need to change regex in /status/123 rule to make it capture only numbers and hyphens and bring /blog/this-is-a-blog-post rule below it.

Use this code:

Options +FollowSymLinks -MultiViews
RewriteEngine On

# Displays directory if there is no / on the end of the URL
RewriteCond %{REQUEST_URI} !^/(admin|status|customer) [NC]
# Removes index.php from URL 
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php$ /$1 [L,R=302,NC,NE]

# Rewrites /services to be /index.php?id=services
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-]+)/?$ /index.php?id=$1 [L,QSA]

# Rewrites /blog/year2013/month12 to be /index.php?id=blog&year=2013&month=01
RewriteRule ^([a-zA-Z0-9-]+)/year([0-9]+)/month([0-9]+)/?$ /index.php?id=$1&year=$2&month=$3 [L,QSA]

# Rewrites /status/123 to be /index.php?id=status&seq=123
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-]+)/([\d-]+)/?$ /index.php?id=$1&seq=$2 [L,QSA]

# Rewrites /blog/this-is-a-blog-post to be /index.php?id=blog&slug=this-is-a-blog-post
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-]+)/([\w-]+)/?$ /index.php?id=$1&slug=$2 [L,QSA]

# Rewrites /status/cat1 to be /index.php?id=status&cat=1
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule ^([a-zA-Z0-9-]+)/cat([0-9]+)?$ /index.php?id=$1&cat=$2 [L,QSA]

Upvotes: 3

Related Questions