Nouman
Nouman

Reputation: 132

hide subdirectory from url with .htaccess

i have following directory structure

Root(www.example.com)/
                  index.php(simple page with links pointing to subdirectory like www.example.com/subdirectory/index.php?id=1)
                  subdirectory/
                              index.php
                              .htaccess(handling all subdirectory rewriting and working fine)

so my current url is

www.example.com/subdirectory/10/

so i want to hide the 'subdirectory' from the the url so the final url should look like

www.example.com/10/

i have tried by adding following rule in my subdirectory/.htaccess file but its not working

RewriteCond %{REQUEST_URI} !(.*)subdirectory
RewriteRule ^(.*)$ subdirectory/$1 [L]

or

RewriteRule ^$ subdirectory/
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ subdirectory/$1

my subdirectory/.htaccess file

RewriteEngine On                                                                                                                              

RewriteRule ^([-]?[0-9]+)([-_][^/]*)?/posts/([0-9]+)([-_][^/]*)?/([0-9]+)([-_][^/]*)?/([0-9]+)([-_][^/]*)?\.html  index.php?view=showad&adid=$7&cityid=$1 [QSA]
RewriteRule ^([-]?[0-9]+)([-_][^/]*)?/posts/([0-9]+)([-_][^/]*)?/([0-9]+)([-_][^/]*)?/page([0-9]*)\.html   index.php?view=ads&catid=$3&subcatid=$5&cityid=$1&page=$7 [QSA]
RewriteRule ^([-]?[0-9]+)([-_][^/]*)?/posts/([0-9]+)([-_][^/]*)?/([0-9]+)([-_][^/]*)?     index.php?view=ads&catid=$3&subcatid=$5&cityid=$1 [QSA]

Upvotes: 1

Views: 1435

Answers (1)

anubhava
anubhava

Reputation: 785196

In your subdirectory/.htaccess, insert these rules:

RewriteEngine On
RewriteBase /subdirectory/

# external redirect from actual URL to a pretty one
RewriteCond %{THE_REQUEST} /subdirectory/(\S*)\s [NC]
RewriteRule ^ %1? [R=301,L,NE]

# remaining rules go below this

You will also need these rules in your site's root .htaccess (create it if it doesn't exist):

RewriteEngine On

RewriteRule ^$ subdirectory/ [L]

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ subdirectory/$1 [L]

Upvotes: 0

Related Questions