Php – URL rewrite to add a directory at start of url

.htaccessphp

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 /newdirectory/CMS/…

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

Best Solution

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
Related Question