Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

URL rewrite to add a directory at start of url

Tags:

php

.htaccess

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?

like image 624
Ash Avatar asked Mar 08 '11 05:03

Ash


People also ask

What is the difference between URL Rewrite and redirect?

Simply put, a redirect is a client-side request to have the web browser go to another URL. This means that the URL that you see in the browser will update to the new URL. A rewrite is a server-side rewrite of the URL before it's fully processed by IIS.

How does IIS URL Rewrite work?

The URL rewriting module runs early in the request-processing pipeline, modifying the requested URL before the Web server decides which handler to use to process the request. The handler, which is chosen based on the rewritten URL, processes the request and generates a response that is sent back to the Web browser.

What is Rewriterule in htaccess?

htaccess rewrite rules can be used to direct requests for one subdirectory to a different location, such as an alternative subdirectory or even the domain root. In this example, requests to http://mydomain.com/folder1/ will be automatically redirected to http://mydomain.com/folder2/.


2 Answers

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
like image 195
Luke Stevenson Avatar answered Sep 24 '22 12:09

Luke Stevenson


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
like image 29
Paul Schreiber Avatar answered Sep 22 '22 12:09

Paul Schreiber