Rewrite URL to add a directory at the beginning of the URL

On my website, all the images / stylesheets are in the / CMS / directory .... Recently, our website switched to a new server with a temporary URL that they referred to as / newdirectory / CMS / ...

How can we add / newdirectory / to all / CMS / calls?

+6
php .htaccess
source share
3 answers

Inside the .htaccess file

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

This will overwrite, so accessing http://www.server.com/CMS/index.html will actually serve the contents of http://www.server.com/newdirectory/CMS/index.html

Note. This solution assumes that the CMS is the only one served for this domain.

If this domain serves more than CMS (and only CMS needs to be redirected), then it might be better:

 RewriteEngine On RewriteCond %{REQUEST_URI} ^/CMS/ RewriteRule ^(.*)$ /newdirectory/$1 
+11
source share

You do not need a RewriteRule (or mod_rewrite) for this. You can use a simple RedirectMatch:

 RedirectMatch ^/CMS/(.*) http://tempserver.com/newdirectory/CMS/$1 
+3
source share

I would 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 will not affect the current value of pagerank, SEO, etc., and all incoming links will be supported.

+1
source share

All Articles