Simon K Bhatta4ya
Simon K Bhatta4ya

Reputation: 1047

.htaccess redirect & hide directory path in url

I have wordpress installed in root directory. I want to use some separate php files as a page on my domain, for which I have made a separate directory which holds all files for serving the php files, of which the directory structure is as:

the root folder has all the wordpress files

the directory which I'd like to serve as a page

/inc/css/    
/inc/php/    
/inc/img/

the CSS stylesheet files directory location in the PHP file is ../inc/css one step back & then css folder. I want to hide folders from URL such as the files are served from the root (hide the /inc/php/, /inc/css/ & /inc/img/ folders from URL).

eg: www.domain.com/inc/php/about.php redirect & rewrite this URL to www.domain.com/about

the .htaccess in my root

RewriteEngine On
RewriteBase /

# disable directory browsing
Options -Indexes

# Prevent hotlinking of images htaccesstools.com/hotlink-protection
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?domain.com [NC]
RewriteRule \.(jpg|jpeg|png|gif|ico|pdf|flv|jpg|jpeg|mp3|mpg|mp4|mov|wav|wmv|png|gif|swf|css|js)$ - [NC,F,L]

RewriteRule ^login$ http://domain.com/wp-login.php [NC,L]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

<files wp-config.php>
order allow,deny
deny from all
</files>

<files ~ "^.*\.([Hh][Tt][Aa])">
order allow,deny
deny from all
satisfy all
</files>

I have tried the simple redirect rule but the folders are exposed in the URL.

Redirect 301 /about.php /inc/php/about.php

also I have some more files in the PHP folder on which I'd like to apply the same rule of redirect & rewrite URL & hide folders from URL & remove the PHP extention.

Upvotes: 0

Views: 5485

Answers (1)

Jon Lin
Jon Lin

Reputation: 143886

www.domain.com/inc/php/about.php redirect & rewrite this URL to www.domain.com/about

This, of course, means that you can't have the same base filename that's a php file and, say, a css file. Since if the request is www.domain.com/about, is that supposed to map to /inc/php/about.php or /inc/css/about.css? Or is it an image? If you have both files, only one will get mapped to.

But if that's really what you want, try adding these rules right after the hotlinking rule that you have:

# Externally redirect requests for /inc/
RewriteCond %{THE_REQUEST} \ /inc/(php|img|css)/([^\?\ ]+)\.(php|css|jpe?g|png|gif) [NC]
RewriteRule ^ /%2 [L,R=301]

# Check if the request is a php file:
RewriteCond %{DOCUMENT_ROOT}/inc/php%{REQUEST_URI}.php -f
RewriteRule ^(.*)$ /inc/php/$1.php [L]

# Check if the request is a css file:
RewriteCond %{DOCUMENT_ROOT}/inc/css%{REQUEST_URI}.css -f
RewriteRule ^(.*)$ /inc/css/$1.css [L]

# Check if the request is a jpeg file:
RewriteCond %{DOCUMENT_ROOT}/inc/img%{REQUEST_URI}.jpg -f
RewriteRule ^(.*)$ /inc/img/$1.jpg [L]

# Check if the request is a gif file:
RewriteCond %{DOCUMENT_ROOT}/inc/img%{REQUEST_URI}.gif -f
RewriteRule ^(.*)$ /inc/img/$1.gif [L]

# Check if the request is a png file:
RewriteCond %{DOCUMENT_ROOT}/inc/img%{REQUEST_URI}.png -f
RewriteRule ^(.*)$ /inc/img/$1.png [L]

Upvotes: 1

Related Questions