开发者

new to mod_rewrite

I'm new to using mod_rewrite and could do with some help, this is what I've managed to get to work so far

Options +FollowSymlinks
RewriteEngine On 

R开发者_JAVA百科ewriteBase /

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

RewriteRule ^(.*)/(.*)/pid/(.*)/(.*)$ /$1/$2.php?np=$3 [NC]
RewriteRule ^news/article/(.*)$ /news/details.php?newstag=$1 [NC]

This works with links such as: http://www.mydomain.co.uk/about/about/pid/2/About (folder/filename/pid/page id/page title

but not with, and I cannot get it to work.

http://www.mydomain.co.uk/index/pid/1/Welcome-to-this-site (filename/pid/page id/title)

Is there a way to make the folder option ^(.*)/ optional?


To make the folder option optional, I would split the rule in two:

RewriteRule ^(.*)/(.*)/pid/(.*)/(.*)$ /$1/$2.php?np=$3 [NC]
RewriteRule ^(.*)/pid/(.*)/(.*)$ /$1.php?np=$2 [NC]

Note that "." match everything, including a "/", for this reason, I guess both rules would match a URL like: http://www.mydomain.co.uk/one/two/three/four/pid/1/Welcome-to-this-site

Where $1 would be: "one/two/three" and $2: "four".

To avoid this, use [^/] rather than ".":

RewriteRule ^([^/]*)/([^/]*)/pid/([^/]*)/([^/]*)$ /$1/$2.php?np=$3 [NC]
RewriteRule ^([^/]*)/pid/([^/]*)/([^/]*)$ /$1.php?np=$2 [NC]

If you don't care about the reachable folder depth, the following rules should be enough and match both with and without a foldername:

RewriteRule ^(.*)/pid/(.*)/(.*)$ /$1.php?np=$2 [NC]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜