.htaccess URL Rewrite to Subdirectory

前端 未结 3 459
北恋
北恋 2020-12-10 14:25

What is the best method to rewrite anything below \"/some/subdir\" to \"/some/subdir/projects\" like from this:

http://www.mydomain.com/some/subidr/test/


        
相关标签:
3条回答
  • 2020-12-10 15:08

    Try this rule in your .htaccess configuration file in the document root of your server:

    RewriteEngine on
    RewriteRule !^some/subdir/projects(/|$) some/subdir/projects%{REQUEST_URI} [L]
    

    As you want to use this rule in your /some/subdir/ directory, change the rule as follows:

    RewriteRule !^projects(/|$) projects%{REQUEST_URI} [L]
    

    And if you want to redirect any requests of /some/subdir/projects/foobar to /some/subdir/foobar, put this rule above the previous mentioned:

    RewriteCond %{THE_REQUEST} ^[A-Z]+\ /some/subdir/projects[/?\s]
    RewriteRule ^some/subdir/projects/?([^/].+)?$ /some/subdir/$1 [L,R=301]
    
    0 讨论(0)
  • 2020-12-10 15:18

    This is the solution I finally got to work:

    RewriteEngine On
    
    RewriteCond %{REQUEST_URI} !^/some/subdir/projects/.*$
    RewriteRule ^(.*)$ /some/subdir/projects/$1 [L]
    
    0 讨论(0)
  • 2020-12-10 15:22

    I would use this:

    RewriteEngine On
    RewriteRule ^(/some/subdir)/(.*)$ $1/projects/$2
    

    This will redirect /some/subdir/<anything> to /some/subdir/projects/<anything>.

    Note that the leading / is actually required to match the beginning of the URL, unless you have RewriteBase / set somewhere.

    0 讨论(0)
提交回复
热议问题