确保使用了rewrite-rule



我创建了一个简单的重写URL,如下所示:

RewriteRule ^messages$ /messages.php [L]

如何强制用户使用/messages而不是/messages.php?例如,如果用户查询messages.php,他将被301重定向到/messages。

我有一长串重写规则,就像上面的一样。

RewriteEngine On
RewriteRule ^messages$ /messages.php [L]
RewriteRule ^events$ /events.php [L]
RewriteRule ^cv$ /cv.php [L]
RewriteRule ^profile/([^/]*)$ /profile.php?id=$1 [L]
RewriteRule ^ratings$ /ratings.php [L]
RewriteRule ^newsfeed$ /newsfeed.php [L]
RewriteRule ^logout$ /logout.php [L]
RewriteRule ^profile$ /profile.php [L]
RewriteRule ^ranking$ /rank.php [L]
RewriteRule ^find-study$ /search.php [L]
RewriteRule ^search/$ /search.php [L]
RewriteRule ^search$ /search.php [L]
RewriteRule ^invite-friends$ /invite-friends.php [L]
RewriteRule ^profile/([^/]*)$ /profile.php?id=$1 [L]
RewriteRule ^profile/([^/]*)/([^/]*)$ /profile.php?id=$1&programme=$2 [L]
RewriteRule ^student-requests$ /student-requests.php [L]
# Q&A
    # view question
        RewriteRule ^question(?:/([^/]+)(?:/([^/]*))?)?/?$ /question.php?id=$1&permalink=$2 [L]
        RewriteRule ^question/([^/]*)/([^/]*)/([^/]*)$ /question.php?id=$1&permalink=$2&answer=$3 [L]
    # manage question
        RewriteRule ^ask(?:/([^/]+)(?:/([^/]*))?)?/?$ /manageQuestion.php?tag_id=$1&tag_type=$2 [L]
        RewriteRule ^ask/([^/]*)/([^/]*)/([^/]*)$ /manageQuestion.php?tag_id=$1&tag_type=$2&tag_name=$3 [L]
        RewriteRule ^editquestion(?:/([^/]+)(?:/([^/]*))?)?/?$ /manageQuestion.php?id=$1&second=$2 [L]
    # questions
        RewriteRule ^questions$ /questions.php [L]
        RewriteRule ^questions/([^/]*)$ /questions.php?first=$1 [L]
        RewriteRule ^questions/([^/]*)/([^/]*)$ /questions.php?first=$1&second=$2 [L]
        RewriteRule ^questions/([^/]*)/([^/]*)/([^/]*)$ /questions.php?first=$1&second=$2&third=$3 [L]
        RewriteRule ^questions/([^/]*)/([^/]*)/([^/]*)/([^/]*)$ /questions.php?first=$1&second=$2&third=$3&fourth=$4 [L]

在尝试解决这个问题时,您可能会遇到无限循环的问题。防止这种情况的一种方法是使用%{THE_REQUEST},因为这个变量在重写时不会改变。因此,如果您使用以下规则,它将把.php上的每个url都更改为不包含该url的url:

RewriteCond %{THE_REQUEST} ^(GET|POST) /(messages|events|cv|ratings|etc).php HTTP
RewriteRule ^ %2 [R,L]

测试该规则正常工作后,将[R,L]更改为[R=301,L]以使重定向永久存在。在一切正常工作之前这样做会让测试变得很痛苦。

编辑:而不是使用(.*)你可以使用/(page1|page2|page3)所有简单的重定向。对于每一个更复杂的,你可能需要有一个单一的重定向形式为:

RewriteCond %{THE_REQUEST} ^(GET|POST) /questions.php?id=([^&]+)&permalink=([^&]+)&answer=([^&]+) HTTP
RewriteRule ^ questions/%2/%3/%4 [R,L]

最新更新