简体   繁体   中英

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?

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.

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 eg if your structure is

/home/site1, /home/site2

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM