用2个带“/”分隔的变量重写规则

时间:2012-10-31 06:14:18

标签: regex apache .htaccess mod-rewrite

我的.htaccess文件位于“/ mixtapes /”文件夹中我试图让网址mydomain.com/music/downloads/mixtapes/this-title/id执行mydomain.com/music/downloads/mixtapes/item.php?title=variable1&id=variable2

我目前有以下方式有点工作,但它只使用id,我需要两个变量(../mixtapes/title/id)分别由“/”和出于某种原因与下面的代码“/ mixtapes /”内的索引页面不起作用。我很难过!我对此有些新意,非常感谢任何帮助!

在我的索引页面上BTW将item.php页面的传递网址重写为<a href="title/id">我似乎无法正确执行item.php?title=a&id=b格式为mixtapes/title/id < / p>

当前的htaccess文件位于“/ mixtapes /”

# turn mod_rewrite engine on 
RewriteEngine On

# rewrite all physical existing file or folder
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d

# allow things that are certainly necessary
RewriteCond %{REQUEST_URI} "/css/" [OR]
RewriteCond %{REQUEST_URI} "/images/" [OR]
RewriteCond %{REQUEST_URI} "/images/" [OR]
RewriteCond %{REQUEST_URI} "/javascript/"

# rewrite rules
RewriteRule ^mixtapes/item.php(.*) - [L]
RewriteRule ^(.*) item.php?id=$1 [QSA]

1 个答案:

答案 0 :(得分:0)

.htaccess中的评论实际上是错误的

# turn mod_rewrite engine on 
RewriteEngine On

# if requested URL is NOT a existing file  
RewriteCond %{REQUEST_FILENAME} !-f [OR]
# or if requested URL is NOT a directory
RewriteCond %{REQUEST_FILENAME} !-d

# CSS, images, JavaScript are files and we will never pass this point when those are requested, next rules can be skipped

# rewrite rules
# rewrite /mixtapes/title/id to item.php?title=title&id=id
Rewrite ^mixtapes/([^/]+)/([0-9]+)  item.php?title=$1&id=$2   [L]

# catch all other requests and handle them ( optional, default would be 404 if not a physically existing file )
Rewrite (.*) index.php  [L,QSA]

我假设你的id是一个数值。 请注意在php中使用标题。请勿直接输出,但您可以使用它来验证您的网址并重定向错误的标题/ ID组合