0

我想仅基于最后一个参数重写一些 URL。例如:

http://www.site.com/some-param/some-param/THIS-param

我想抓住THIS-param并使用它进行重写。它始终是最后一个参数,但不一定是第三个。可能是第 2、第 3、第 4 或第 5。

我正在接近这个:

RewriteRule ([^/]+)$ index.php?url_title=$1 [NC,L,QSA]

使用这个作为 url(例如)。

http://www.test.com/param1

问题是,一旦我添加了“param2”,网站就会停止工作。我认为这是因为 param1 似乎是一个目录。

http://www.test.com/param1/param2

知道为什么吗?这是我的全套规则:

Options +FollowSymlinks -MultiViews

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} -f [NC,OR] 
RewriteCond %{REQUEST_FILENAME} -d [NC] 
RewriteRule .* - [L]

RewriteRule ([^/]+)$ index.php?url_title=$1 [NC,L,QSA]
RewriteRule .* index.php [L]
4

1 回答 1

0

You nearly had it. You need to capture everything that comes after the last /, and optionally capture what comes before it if needed. You said it coudld be 2nd through 5th, but didn't mention 1st, so that assumes something comes before it with a /, as in ^.+/.

This also allows for a trailing /

Edited to incorporate existing rules:

RewriteEngine On

# Real existing files not rewritten
RewriteCond %{REQUEST_FILENAME} -f [NC,OR] 
RewriteCond %{REQUEST_FILENAME} -d [NC] 
RewriteRule .* - [L]

#Inelegant hack to permit one param only
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?url_title=$1 [L,QSA]
RewriteRule ^.+/([^/]+)/?$ index.php?url_title=$1 [L,QSA]
# Anything not matching above goes to index.php
RewriteRule .* index.php [L]
于 2012-08-02T13:50:12.877 回答