0

Using .htaccess apache rewrite I'm looking to remove subfolder/substring from the my url. For now it looks like:

www.domain.com/store/products.php`

and I want to change it to:

www.domain.com/products.php

How can i do it using .htaccess apache rewrite?

1
  • What code do you have? Please post.
    – Lizz
    Dec 11, 2014 at 17:44

2 Answers 2

7

You can do that with the root .htaccess:

RewriteEngine on 
RewriteBase / 
RewriteRule ^store/(.*) /$1 [R=302,L]

Change [R=302] for [R=301] when test work well.

0
1

You need mod_rewrite. A rule will be something like this:

RewriteEngine On
RewriteBase /myapp/
RewriteRule ^/store/products\.php$ /products.php

Or, for many different files, e.g. products.php, places.php, blablabla.php:

RewriteEngine On
RewriteBase /myapp/
RewriteRule ^/store/([a-z]+)\.php$ /$1.php

That depends on what URLs you want to rewrite. You should read the docs.

All-matching .htaccess rule

Also, I'd suggest you to use the following catch all URLs pattern. It will redirect any path to index.php. This is widely used in a lot of CMS systems to give you ability to perform dynamic routing with PHP:

.htaccess

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?path=$1 [L,QS

index.php

<?php

$path = isset($_GET['path']) ? explode('/', $_GET['path']) : array();

// If you navigate to /foo/bar/dead/beef, yur $path var will be like:
// $path == array ( 0 => 'foo', 1 => 'bar', 2 => 'dead', 3 => 'beef' );

?>
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.