Why if I put a “-” dash in a rule in my htaccess d

2020-02-07 04:10发布

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]

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?

Thank you.

3条回答
Lonely孤独者°
2楼-- · 2020-02-07 04:27

I suspect that you have an actual file (or more likely an empty directory) named our-stores.

This would trigger the RewriteCond %{REQUEST_FILENAME} !-d, resulting in it not rewriting.

It would then try to load the default page from that directory (eg index.php), which doesn't exist, and so it would try to rewrite that, resulting in the ?sub=our-stores&second= paramters being added.

查看更多
祖国的老花朵
3楼-- · 2020-02-07 04:33

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 .*.

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]
查看更多
欢心
4楼-- · 2020-02-07 04:35

The sign "-" is a special character in regular expression. It defines a range (a-z, à-9, etc).

Try to escape it.

RewriteRule ^our\-stores$ /our-stores.php [L,NC]
查看更多
登录 后发表回答