最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

regex - Can't redirect with .htaccess redirect rule - Stack Overflow

programmeradmin4浏览0评论

Can explain me why this rule not working.

Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule \/get-info\/plugin\/(.*) \/repositories\/get-info.php?plugin=$1

I want when someone access to:

www.example/get-info/plugin/test

go to:

www.example/repositories/get-info.php?plugin=test

Can explain me why this rule not working.

Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule \/get-info\/plugin\/(.*) \/repositories\/get-info.php?plugin=$1

I want when someone access to:

www.example/get-info/plugin/test

go to:

www.example/repositories/get-info.php?plugin=test
Share Improve this question edited Mar 19 at 15:58 MrWhite 46.1k8 gold badges64 silver badges88 bronze badges asked Mar 17 at 13:24 pedrofernandespedrofernandes 16.9k10 gold badges37 silver badges44 bronze badges 3
  • The escaping of slashes via backslashes is simply wrong. In the pattern, as well as in the substitution URL. – C3roe Commented Mar 17 at 13:28
  • And you probably want to anchor the pattern at the start at least. And in .htaccess context, the URL path RewriteRule matches against, never starts with a slash. Use RewriteRule ^/?get-info/plugin/(.*) /repositories/get-info.php?plugin=$1 with an optional slash at the start for a rule that will work in both .htaccess as well as in server config/vhost. – C3roe Commented Mar 17 at 13:30
  • "The escaping of slashes via backslashes is simply wrong." - Although that's not an error, it's simply unnecessary. (cPanel, for instance, generates code with slashes in the substitution string backslash-escaped.) @C3roe – MrWhite Commented Mar 19 at 16:07
Add a comment  | 

1 Answer 1

Reset to default 1

Your current .htaccess rule isn’t working because:

  1. Incorrect Escaping of Slashes
    You don't need to escape slashes (/) in RewriteRule. Apache processes them as normal characters.

  2. Missing Start Anchor (^)
    In .htaccess, the URL path being matched never starts with a leading slash. Your rule should start with ^ to properly match from the beginning.

  3. Recommended Fix
    Try this corrected rule:

    Options +FollowSymLinks
    RewriteEngine on
    RewriteBase /
    RewriteRule ^get-info/plugin/(.*)$ repositories/get-info.php?plugin=$1 [L,QSA]
    

    Explanation:

    • ^get-info/plugin/(.*)$ → Matches /get-info/plugin/anything correctly.

    • repositories/get-info.php?plugin=$1 → Redirects to the correct script.

    • [L,QSA]:

      • L (Last) → Stops processing further rules if this one matches.

      • QSA (Query String Append) → Ensures existing query parameters aren’t lost.

This should correctly rewrite www.example/get-info/plugin/test to www.example/repositories/get-info.php?plugin=test.

发布评论

评论列表(0)

  1. 暂无评论