ElieH
ElieH

Reputation: 105

htaccess rewrite rule with multiple parameters

This is my .htaccess file:

Options +FollowSymLinks
Options -Multiviews

RewriteEngine On
RewriteBase /website/

RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [S=20]

RewriteRule ^(.*)$ $1.php 
RewriteRule ^([a-z0-9_-]+)/([a-z0-9_-]+)(/)?$ index.php?occ=$1&cat=$2

now these 2 lines together are not working for example:

if I were to go to contact.php, I would type in my url: localhost/website/contact and it would work

however if I want to go to location/website/index.php?occ=occasion&cat=category by typing this in my url: localhost/website/occasion/category it will only work if I remove the first rewrite rule

but then I cant go to localhost/website/contact anymore

what am I missing here?

Upvotes: 2

Views: 6113

Answers (2)

Ankit Pise
Ankit Pise

Reputation: 1259

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^site_1/(.*)$ site_1/index.php?/$1 [L]
RewriteRule ^site_2/(.*)$ site_2/index.php?/$1 [L]

as in sites you won't have 1000s of sites you can declare your desire link and folder name e.g. if your structure is

/home/site1, /home/site2

and links will be like http://yourdomain.com/medical & http://yourdomain.com/entertainment

Upvotes: 0

Cobra_Fast
Cobra_Fast

Reputation: 16061

Your Problem is your order.

/website/occasion/category first gets rewritten to be /website/occasion/category.php, which then fails to match the second rule.

RewriteRule ^([a-z0-9_-]+)/([a-z0-9_-]+)(/)?$ index.php?occ=$1&cat=$2 [L]
RewriteRule ^(.*)$ $1.php

Changing the order and adding the [L] parameter to the rule (stop executing followup rules after succesful execution) should fix the issue.

Upvotes: 5

Related Questions