Using rewrite rules across multiple virtual hosts

Using a single web hosting account to host multiple sites

Postby richardk » Sun Dec 21, 2008 9:49 am

Try the mod_rewrite test. (Put the mod_rewrite in the server configuration and check that it is inherited by the <VirtualHost>s.)

What are your <VirtualHost>s like? What are the sub domain document roots like?

Also try
Code: Select all
RewriteEngine On

RewriteRule !(\.[^/]+|[^/])$ %{REQUEST_URI}.php [PT,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby AbuAaminah » Sun Dec 21, 2008 9:05 pm

The rewrite test helped me to realise that RewriteEngine On had to go in each virtual host. Doing that has made rewrites work.

Now I seem to be having a problem with the rewrites. (Note: Your amazing help is truly appreciated.) :

---------------------------------------------------------------

#
# Get rid of '.php':
#
RewriteCond %{THE_REQUEST} \.php
RewriteCond %{THE_REQUEST} !\?.*\.php
RewriteRule ^([^\?]+)\.php(\?.*)?$ $1$2 [R=301,L]

#
# Rewrite extensionless URLs to ".php" page
#
RewriteCond %{REQUEST_URI} !^(.*)/$
RewriteCond %{REQUEST_URI} ^(.*)$
RewriteRule !(\.)(.*)$ %1.php [L]

#
# Get rid of 'index' in directories:
#
RewriteCond %{THE_REQUEST} index
RewriteCond %{THE_REQUEST} !\?.*\index
RewriteRule ^([^\?]*)index\.php(\?.*)?$ $1$2 [R=301,L]

---------------------------------------------------------------

These rewrites are supposed to:
1. Force any attempt at viewing a page with .php at the end of the URI redirect to the non-php version.
2. Load up the php version of an extensionless URI if it does not belong to a directory.
3. Redirect people who visit /any-directory/index.php to /any-directory/

Whilst all of these are working fine, the one which it does not cover is:
4. Redirect people who visit /any-directory/index to /any-directory/.

Any ideas on that one?

A million thanks once again.
AbuAaminah
 
Posts: 40
Joined: Mon Dec 08, 2008 6:31 pm

Postby richardk » Mon Dec 22, 2008 4:15 pm

Code: Select all
^([^\?]*)index\.php(\?.*)?$

limits the whole rule set to index.php only.

Try
Code: Select all
#
# Get rid of '.php':
#
RewriteCond %{THE_REQUEST} \ /[^\?]+\.php(\?.*)?\  [NC]
RewriteRule ^(/.+)\.php$ $1 [NC,R=301,L]

#
# Rewrite extensionless URLs to ".php" page
#
RewriteRule !(\..+|/)$ %{REQUEST_URI}.php [L]

#
# Get rid of 'index' in directories:
#
RewriteCond %{THE_REQUEST} \ /[^\?]+index(\.php)?(\?.*)?\  [NC]
RewriteRule ^(/(.+/)?)index(\.php)?$ $1 [NC,R=301,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby AbuAaminah » Mon Dec 22, 2008 6:56 pm

THanks for your help...

1. Get rid of .php one...

Does not work at all.

2. Rewrite extensionless url's to .php...

In addition to rewriting extensionless file names, its rewriting directories. (I'm going to open a separate thread for this one because it gets more complicated.)

3. Get rid of index and index.php.

This only works for index.php, but not index.

Interestingly, the original one works when it is in an htaccess with RewriteBase /, but not in httpd.conf. Perhaps RewriteBase needs to be set in the httpd.conf file as well (not sure where though).

Thanks for your ongoing help Richard, you are a super star.
AbuAaminah
 
Posts: 40
Joined: Mon Dec 08, 2008 6:31 pm

Postby richardk » Tue Dec 23, 2008 9:41 am

Try (tested on Apache 2.2, what version of Apache are you using?)
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{THE_REQUEST} \ [^/\?]+//[^/\?]+/([^\?]+/)?index(\..+|\?.*)?\  [NC]
RewriteRule . /%1 [R=301,L]

# Remove the .php from PHP files (only files
# that exist).
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{DOCUMENT_ROOT}%{SCRIPT_FILENAME} -f
RewriteRule ^(/.+)\.php$ $1/ [R=301,L]

# Rewrite /<name and /<name>/ to /<name>.php if
# a PHP file exists and a directory does not.
RewriteCond %{DOCUMENT_ROOT}$1.php -f
RewriteCond %{DOCUMENT_ROOT}%{SCRIPT_FILENAME} !-d
RewriteRule ^(/.*[^/])/?$ $1.php [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby richardk » Tue Dec 23, 2008 10:39 am

Sorry this is the thread to talk about the <VirtualHost> version of the mod_rewrite. I edited the wrong post.

AbuAaminah wrote:The good news is that the following works in its entirety:
Code: Select all
#
# Remove the inex.php
#
RewriteCond %{THE_REQUEST} index
RewriteCond %{THE_REQUEST} !\?.*\index
RewriteRule ^([^\?]*)index\.php(\?.*)?$ $1$2 [R=301,L]

# Remove the .php from PHP files (only files that exist).
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{SCRIPT_FILENAME} -f
RewriteRule ^(.+)\.php$ /$1/ [R=301,L]

# Redirect  /<name to /<name>/ if a PHP file or directory exists.
RewriteCond %{DOCUMENT_ROOT}/$1.php -f [OR]
RewriteCond %{SCRIPT_FILENAME} -d
RewriteRule ^(.*[^/])$ /$1/ [R=301,L]

# Rewrite /<name and /<name>/ to /<name>.php if a PHP file exists and a directory does not.
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*[^/])/?$ /$1.php [QSA,L]

Your one-line index removal doesn't work at all, but the one above does.

I'm now trying this in the httpd.conf file and will report back ASAP.


The not-working index redirect.
Code: Select all
RewriteCond %{THE_REQUEST} \ [^/\?]+//[^/\?]+/([^\?]+/)?index(\..+|\?.*)?\  [NC]
RewriteRule . /%1 [R=301,L]


AbuAaminah wrote:Okay. Mixed results when placed in conf file...

1. /directory/file => //directory/file/ (5 sec delay and additional slash added before /directory)
2. /directory/file.php => /directory/file.php
3. /directory/directory => /directory/directory/
4. /directory/index.php => /directory/
5. /directory/index => //directory/index/ (5 sec delay and additional slash added before /directory)

(Note: This is my desktop, localhost, in which 5 sec delays are taking place)


Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{THE_REQUEST} \ /([^\?]+/)?index(\..+|\?.*)?\  [NC]
RewriteRule . /%1 [R=301,L]

# Remove the .php from PHP files (only files
# that exist).
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{DOCUMENT_ROOT}%{SCRIPT_FILENAME} -f
RewriteRule ^(/.+)\.php$ $1/ [R=301,L]

# Redirect  /<name to /<name>/ if a PHP file
# or directory exists.
RewriteCond %{DOCUMENT_ROOT}$1.php -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{SCRIPT_FILENAME} -d
RewriteRule ^(/.*[^/])$ $1/ [R=301,L]

# Rewrite /<name and /<name>/ to /<name>.php if
# a PHP file exists and a directory does not.
RewriteCond %{DOCUMENT_ROOT}$1.php -f
RewriteCond %{DOCUMENT_ROOT}%{SCRIPT_FILENAME} !-d
RewriteRule ^(/.*[^/])/$ $1.php [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby AbuAaminah » Tue Dec 23, 2008 11:21 am

FANTASTIC!!!

They all work. If you weren't a man, not only would I kiss you, but I would propose to you as well.

You are the man!
AbuAaminah
 
Posts: 40
Joined: Mon Dec 08, 2008 6:31 pm

Postby AbuAaminah » Tue Dec 23, 2008 11:25 am

One last thing... the second one which removes ".php"... one of the objectives of this whole thing was to achieve a bit of security through obscurity.

How can that be modified so that "asp", "jsp", "htm" & "html" extensions also redirect to their extensionless equivalents?
AbuAaminah
 
Posts: 40
Joined: Mon Dec 08, 2008 6:31 pm

Postby richardk » Wed Dec 24, 2008 8:52 am

Do you just want the extensions removed or do you want /htmlfile to go to /htmlfile.html and /aspfile to go to /aspfile.asp, etc. What should happen if you go to /somefile and there is a /somefile.php and a /somefile.asp? What order should it be checked?
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby AbuAaminah » Wed Dec 24, 2008 8:55 am

If someone visit /file.php or /file.asp or /file.htm or /file.jsp, they should be redirected to /file/

If /file/ does not then resolve to a php file or a directory, then throw a 404.
AbuAaminah
 
Posts: 40
Joined: Mon Dec 08, 2008 6:31 pm

PreviousNext

Return to Domain Handling

Who is online

Users browsing this forum: No registered users and 25 guests

cron