如何从mod_rewrite中转义“/”

时间:2015-07-06 18:43:01

标签: apache .htaccess mod-rewrite

这是我的.htaccess文件thnx到@ Gy​​rocode-com

/usr/include

一切都很好,除非我尝试去这个链接

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^p/(.*)$ fun.php?p=$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^u/(.*)$ user.php?u=$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^u/([^/]+)/([1-9]+)$ user.php?c=$1&p=$2 [L]    #my problem is here

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([1-9]+)$ index.php?p=$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ index.php?c=$1&p=$2 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ index.php?c=$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ /jokes/$1/ [R]

我想把我带到localhost/u/user1/2

的第2页

但它将user1作为变量(u)的值

我想要的是user1/2

1 个答案:

答案 0 :(得分:1)

您的规则可以重构为:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

RewriteRule ^p/([^/]+)/?$ fun.php?p=$1 [L]

RewriteRule ^u/([^/]+)/?$ user.php?u=$1 [L]

RewriteRule ^u/([^/]+)/([1-9]+)$ user.php?c=$1&p=$2 [L]

RewriteRule ^([1-9]+)$ index.php?p=$1 [L]

RewriteRule ^([^/]+)/([^/]+)$ index.php?c=$1&p=$2 [L]

RewriteRule ^([^/]+)/$ index.php?c=$1 [L]

RewriteRule ^([^/]+)$ /jokes/$1/ [R,L]
  1. 使用([^/]+)代替(.*)限制与/之前的任何内容的匹配
  2. 不是在每个规则之前使用RewriteCond %{REQUEST_FILENAME} -fRewriteCond %{REQUEST_FILENAME} -d,而是最好在单独的规则中使用它,如图所示。