Alexander Wigmore
Alexander Wigmore

Reputation: 3186

Rewrite 1 level of url directory

I'm looking to rewrite the first directory of a url string and have the rest of the request still work.

Eg: I want it so when a user clicks the link for : /products/category/item.php it actually grabs the file of : /shop/category/item.php But still shows as /products/category/item.php as the URL

This will be dynamic so it should be something like /products/$ /shop/$1 I'm guessing.

Upvotes: 0

Views: 693

Answers (3)

regilero
regilero

Reputation: 30496

You do not need mod_rewrite. when to avoid mod_rewrite.

Mapping url directories to file directories is a basic functionnality of Apache handled by the mode mod_alias (which is quite certainly already present for you).

So basically you have the Alias and AliasMatch directives. In your case the first one is enough:

Alias /products/ /path/to/web/document/root/shop/

The mapping is done only server-side so the url seen by the end user is never modified.

Upvotes: 2

anubhava
anubhava

Reputation: 785146

Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^products/(.+)$ /shop/$1 [L,NC]

Upvotes: 1

yasu
yasu

Reputation: 1364

Try this:

RewriteEngine On
RewriteRule ^products/(.*)  shop/$1 [L]

Upvotes: 1

Related Questions