为什么如果我放一个“-"在我的 htaccess 规则中破折号不起作用? [英] Why if I put a "-" dash in a rule in my htaccess doesn't work?
问题描述
我有以下规则,our-stores"重定向到与其他PLAIN PAGES"规则不同的地方:
I have the following rules, the one "our-stores" redirects to a different place than the other "PLAIN PAGES" rules:
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RedirectMatch 302 /construction.html http://www.{website}.com/construction-services
RedirectMatch 302 /construction/endless-pools.html http://www.{website}.com/construction-services/endless-pools
# PRODUCT AND SERVICES
RewriteCond %{REQUEST_URI} !\.html$
RewriteRule ^(.*)/(.*)$ /rewrite.php?sub=$1&second=$2 [NC]
# PLAIN PAGES
#RewriteRule ^/$ /index.php [L,NC]
RewriteRule ^signup-free$ /signup-free.php [L,NC]
RewriteRule ^about$ /content.php?page=1 [L,NC]
RewriteRule ^links$ /content.php?page=2 [L,NC]
RewriteRule ^portfolio$ /portfolio.php [L,NC]
RewriteRule ^our-stores$ /our-stores.php [L,NC]
RewriteRule ^contact$ /contact.php [L,NC]
RewriteRule ^products-list$ /lists.php?action=products [L,NC]
RewriteRule ^services-list$ /lists.php?action=services [L,NC]
当我输入 /our-stores
时,apache 重定向到:/our-stores/?sub=our-stores&second=
When I type /our-stores
apache redirects to: /our-stores/?sub=our-stores&second=
我不明白,因为其余的(免注册、关于、链接、作品集……等等)都运行良好.
I don't get it because the rest (signup-free, about, links, portfolio... and so on) are working fine.
有人可以帮我解决这个问题吗?
Can anybody help me with this one?
谢谢.
推荐答案
通用规则已移至底部,REQUEST_FILENAME
条件移至静态重定向下方.此外,我添加了一个 /?
以匹配每个特定重定向上的可选尾部斜杠.最后,改进了通用规则,不再使用 .*
,而是以一种不那么贪婪的方式匹配第一个 /
之前的所有内容.然后它在 /
之后使用 .+
来确保至少存在一个字符.因此,带有斜杠的 URL 将与它不匹配.如果您有一些没有 second=
的泛型,请改回 .*
.
The generic rule has been moved to the bottom, and the REQUEST_FILENAME
conditions moved beneath the static redirects. Further, I have added a /?
to match an optional trailing slash on each of the specific redirects. Finally, the generic rule is improved not to use .*
, but rather to match everything up to the first /
in a less greedy way. Then it uses .+
after the /
to make sure at least one character is present. Urls with a trailing slash would therefore not match it. If you have some generics without a second=
, change back to .*
.
所有这些都在 htaccess 测试器中正常工作.
All of these work correctly in the htaccess tester.
RedirectMatch 302 /construction.html http://www.{website}.com/construction-services
RedirectMatch 302 /construction/endless-pools.html http://www.{website}.com/construction-services/endless-pools
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^signup-free/?$ /signup-free.php [L,NC]
RewriteRule ^about/?$ /content.php?page=1 [L,NC]
RewriteRule ^links/?$ /content.php?page=2 [L,NC]
RewriteRule ^portfolio/?$ /portfolio.php [L,NC]
RewriteRule ^our-stores/?$ /our-stores.php [L,NC]
RewriteRule ^contact/?$ /contact.php [L,NC]
RewriteRule ^products-list/?$ /lists.php?action=products [L,NC]
RewriteRule ^services-list/?$ /lists.php?action=services [L,NC]
# PRODUCT AND SERVICES
RewriteCond %{REQUEST_URI} !\.html$
RewriteRule ^([^/]+)/(.+)$ /rewrite.php?sub=$1&second=$2 [NC]
这篇关于为什么如果我放一个“-"在我的 htaccess 规则中破折号不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!