URL rewrite to add a directory at start of url

前端 未结 3 1326
轮回少年
轮回少年 2021-01-05 12:22

On my website, all images/stylesheets are in the /CMS/... directory. Recently, our website shifted to new server at a temporary url where they referenced like /newdirecto

相关标签:
3条回答
  • 2021-01-05 12:43

    You don't need a RewriteRule (or mod_rewrite) for this. You can use a simple RedirectMatch:

    RedirectMatch ^/CMS/(.*) http://tempserver.com/newdirectory/CMS/$1
    
    0 讨论(0)
  • 2021-01-05 12:47

    Inside the .htaccess file

    RewriteEngine On
    RewriteCond %{REQUEST_URI} !^/newdirectory/CMS/
    RewriteRule ^(.*)$ /newdirectory/CMS/$1
    

    This will perform a rewrite, so accessing http://www.server.com/CMS/index.html will actually serve the content of http://www.server.com/newdirectory/CMS/index.html

    Note: This solution assumes that the CMS is the only thing being served for this domain.

    If this domain is serving more than the CMS (and only the CMS should be redirected), then the following may be better:

    RewriteEngine On
    RewriteCond %{REQUEST_URI} ^/CMS/
    RewriteRule ^(.*)$ /newdirectory/$1
    
    0 讨论(0)
  • 2021-01-05 13:02

    I'd suggest using mod_rewrite, which most apache/linux servers have. Create a file called .htaccess in the docroot of your site with the contents:

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^/CMS/(.*)$      /newdirectory/CMS/$1    [QSA]
    </IfModule>
    

    This method will make it transparent to the end user and no impact on current pagerank, SEO, etc value and all inbound links will be maintained.

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