nested mod_rewrite in .htaccess files

New to mod_rewrite? This is a good place to start.

nested mod_rewrite in .htaccess files

Postby tecktalkcm0391 » Fri May 29, 2009 7:42 am

Is it possible to make a .htaccess file in the root directory with mod_rewrite rules, then have a subdirectory with more rules, but have the subdirectory use both the root and subdirectory rewrite rules.... I can't figure out how to do this if it is possible... Thanks!
tecktalkcm0391
 
Posts: 10
Joined: Tue Jun 19, 2007 7:51 pm
Location: In front of my computer... when I am online...

Postby richardk » Fri May 29, 2009 8:59 am

Put
Code: Select all
RewriteOptions Inherit

in the sub directory .htaccess file.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby tecktalkcm0391 » Fri May 29, 2009 9:49 am

This is the code for my .htaccess file in the root folder, and then the .htaccess in the subfolder has only the second code... But when I go to site.com/index (without the .php because of the rewrite rules below)... I get directed to site.com/root/site.com/html/root/site.com/html

How can I fix this?

Code: Select all
RewriteEngine On
Options +FollowSymLinks

#remove www
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

# remove multiple slashes
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]

# remove .php; use THE_REQUEST to prevent infinite loops
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]

# remove index
RewriteRule (.*)/index$ $1/ [R=301]

# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]

# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]




Code: Select all
RewriteOptions Inherit
RewriteEngine On
tecktalkcm0391
 
Posts: 10
Joined: Tue Jun 19, 2007 7:51 pm
Location: In front of my computer... when I am online...

Postby richardk » Sat May 30, 2009 10:00 am

You don't need the .htaccess file in the sub directory.

You can't redirect to relative paths. Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

#remove www
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

# remove multiple slashes
RewriteCond %{REQUEST_URI} ^(.*/)/(.*)$
RewriteRule . %1%2 [R=301,L]

# Remove /index, /index.html, /index.htm and /index.php
RewriteCond %{THE_REQUEST} \ /(.+/)?index(\.(html?|php))?(\?.*)?\  [NC]
RewriteRule ^(.+/)?index(\.(html?|php))?$ /%1 [NC,R=301,L]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{SCRIPT_FILENAME} -f
RewriteRule ^(.+)\.php$ /$1 [NC,R=301,L]

# remove slash if not directory
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R=301,L]

# add .php to access file, but don't redirect
RewriteCond %{SCRIPT_FILENAME}.php -f
RewriteRule ^(.*[^/])$ /$1.php [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby tecktalkcm0391 » Tue Jun 16, 2009 7:05 pm

This code is still not working... when I go to www.site.com it redirects to site.com/home/domains/site.com/html

This code is in the domains folder and applies to all domains...
Code: Select all
#remove www
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]


This code is in the html folder and applies only to the current domain...
Code: Select all
RewriteBase /
RewriteOptions Inherit
RewriteEngine On
tecktalkcm0391
 
Posts: 10
Joined: Tue Jun 19, 2007 7:51 pm
Location: In front of my computer... when I am online...

Postby richardk » Wed Jun 17, 2009 11:12 am

Try replacing
Code: Select all
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

with
Code: Select all
RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [R=301,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby tecktalkcm0391 » Wed Jul 22, 2009 4:50 pm

Thanks so much! It worked!
tecktalkcm0391
 
Posts: 10
Joined: Tue Jun 19, 2007 7:51 pm
Location: In front of my computer... when I am online...

Postby tecktalkcm0391 » Wed Jul 22, 2009 4:59 pm

The only problem I'm having now is with:

Code: Select all
# remove slash if not directory
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R=301,L]

It keeps going to the root folder, and not the subfolder that is holding the domain files...

ie. /home/domains/domain.com/html has all of domain.com folder and if you go to domain.com/test/ it fowards to domain.com/domain.com/html
(the .htaccess file is in the domains folder)
tecktalkcm0391
 
Posts: 10
Joined: Tue Jun 19, 2007 7:51 pm
Location: In front of my computer... when I am online...

Postby richardk » Thu Jul 23, 2009 9:17 am

Try replacing it with
Code: Select all
# remove slash if not directory
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(/.*[^/])/+$
RewriteRule ./$ %1 [R=301,L]

REQUEST_URI should contain the correct path.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Beginner's Corner

Who is online

Users browsing this forum: No registered users and 26 guests

cron