What's wrong with my RewriteRule via .htaccess? (Why does it need RewriteBase?)

前端 未结 2 624
rewriteengine on
rewriterule ^/a/b$ ^/c$

not working,but

rewriteengine on
rewritebase /
rewriterule ^a/b$ ^c$

wo

相关标签:
2条回答
  • 2021-01-05 13:48

    It's probably not the RewriteBase that makes the rule work so much as the leading slash. Also, the second argument to RewriteRule isn't a regular expression. Instead, try:

    RewriteRule ^/?a/b$ c
    

    When applying a RewriteRule from .htaccess, the leading slash will be stripped from the URL, which will cause the pattern to fail if you include it. By starting a pattern with "^/?", it will work in the main configuration files and in per-directory config files.

    Read over the detailed mod_rewrite documentation for the details of how the rewrite engine works and the significance of RewriteBase.

    Edit: As mentioned in the mod_rewrite technical details and described in the documentation for RewriteRule and RewriteBase, the URL has been translated to a path by the time the per-directory rewrite rules are evaluated. The rewrite engine no longer has a URL to work with. Instead, it removes the local directory prefix (the directory holding the .htaccess file), which ends with a slash. For example, suppose a visitor requests "/var/www/foo/bar/baz.html" and there is a rewrite rule set in "/var/www/foo/.htaccess". Fore each rule, the rewrite engine will strip "/var/www/foo/", leaving "bar/baz.html" to be matched against the rewrite rule. After processing a rule, the local directory prefix is prepended (unless the replacement begins with "http://"). After all the rewriting rules have been processed, the rewrite base, if set, replaces the local directory prefix; if not, the document root is stripped. The rewritten URL is then re-injected as a sub-request.

    0 讨论(0)
  • 2021-01-05 13:51

    What version of Apache are you using? RewriteBase should not be necessary when you are rewriting from the root. If you are not, you may need it. For instance, a part of my current configurations (Apache 2.2) for one of my blogs looks as follows, and works:

    RewriteEngine On    
    RewriteRule ^/$ /blog/ [R]
    RewriteRule ^/wordpress/(.*) /blog/$1 [R]
    
    0 讨论(0)
提交回复
热议问题