Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
290 views
in Technique[技术] by (71.8m points)

php - Why if I put a "-" dash in a rule in my htaccess doesn't work?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...