Complex mod_rewrite URL rewriting - help needed

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,

I'm trying to achieve three different tasks by the way of mod_rewrite
under apache 2.0.54:

 1) permanently redirect (301) url of the form
www.domain.com/index.php?t=key1&sub=key2 
to
www.domain.com/key1/key2

Purpose: let current SERP use 'static' url instead of the old "dynamic"
ones

 2) rewrite url of the form
www.domain.com/key1/key2
to
www.domain.com/index.php?t=key1&sub=key2

Purpose: the site still have to run with dynamic url

 3) move secondary domains under the primary:

before:
www.domain.com/
www.subdomain1.com/
www.subdomain2.com/
...

To
www.domain.com
www.domain.com/subdomain1/
www.domain.com/subdomain2/
...

Purpose: move content that was in other virtual host into the same main
domain, without rewriting the applications.

I'm struggling to death with mod_rewrite rules.
Here is what I currently have that almost work

<VirtualHost *:80>
  ServerAdmin webmaster@xxxxxxxxxx
  ServerName  www.domain.com

  DocumentRoot /var/www/www.domain.com

  RewriteEngine On
  RewriteLog    /var/log/apache2/rewrite.log
  RewriteLogLevel 10

  # Section 1) rewrites
	
  # t=key1&sub=key2 -> /key1/key2/
  RewriteCond %{QUERY_STRING} !t=heading
  RewriteCond %{QUERY_STRING}	^(.*?)&?\bt\b=([^&]+)&\bsub\b=([^&]+)&?(.*)$
  RewriteRule ^(.*)/(index\.php)?$ $1/%2/%3/?%1%4 	[L,R=301]

  # t=key1 -> /key1/
  RewriteCond %{QUERY_STRING} !t=heading
  RewriteCond %{QUERY_STRING}	^(.*?)&?\bt\b=([^&]+)&?(.*)$
  RewriteRule ^(.*)/(index\.php)?$ $1/%2/?%1%3	[L,R=301]

  # Section 2) rewrite
  # static url to real dynamic
  RewriteCond %{REQUEST_FILENAME} !\.(htc|css|jpg|pdf|gif|png|js)$
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(/(subdomain1|subdomain2))?/([^/]+)/([^/]+)/?(index.php)?$ $1/index.php?t=$3&sub=$4 	[QSA,S=1]
	
  RewriteCond %{REQUEST_FILENAME} !\.(htc|css|jpg|pdf|gif|png|js)$
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(/(subdomain1|subdomain2))?/([^/]+)/?(index.php)?$  $1/index.php?t=$3 [QSA]

  # Section 3) Rewrite	
  RewriteRule ^/(subdomain1|subdomain2)(.*)$ /var/www/www.$1.com/$2 [QSA,L]

  <Directory "/var/www/www.domain.com">
    Options +FollowSymlinks
    AllowOverride None
    Order allow,deny
    Allow from all
  </Directory>	

  <Directory "/var/www/www.subdomain1.com">
    Options +FollowSymlinks
    AllowOverride None
    Order allow,deny
    Allow from all
  </Directory>	

  <Directory "/var/www/www.subdomain2.com">
    Options +FollowSymlinks
    AllowOverride None
    Order allow,deny
    Allow from all
  </Directory>	
</VirtualHost>


It almost work, but there are case where it simply doesn't work, mainly:
www.domain.com/subdomain1/index.php
(without any query string paramaters).

The problem is this case comes from:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

Which doesn't match because, of:
172.16.10.4 - - [29/Mar/2007:11:58:22 +0200] [www.domain.com/sid#81dfd20][rid#9e272e0/initial] (4) RewriteCond: input='/subdomain1/index.php' pattern='!-f' => matched
172.16.10.4 - - [29/Mar/2007:11:58:22 +0200] [www.domain.com/sid#81dfd20][rid#9e272e0/initial] (4) RewriteCond: input='/subdomain1/index.php' pattern='!-d' => matched

The file does exist but in /var/www/www.subdomain1.com/ and not in
document_root/subdomain1/, so the rewriteRule doesn't trigger.


I tried to move those rules to per-directory (for instance into the
Directory section of subdomain1):
RewriteCond %{REQUEST_FILENAME} !\.(htc|css|jpg|pdf|gif|png|js)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?(index.php)?$ /var/www/www.subdomain1.com/index.php?t=$1&sub=$2 	[QSA,L,PT]
	
RewriteCond %{REQUEST_FILENAME} !\.(htc|css|jpg|pdf|gif|png|js)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?(index.php)?$ /var/www/www.subdomain1.com/index.php?t=$1	[QSA,L,PT]

It doesn't work either, because when the rewrite rule is applied, Apache
does an Internal Redirect and it triggers the virtual host rules of
Section 1) so the url gets rewritten one more time and I lose
the /subdomain1/.

I'd really appreciate if someone could help me fix this issue.
Thanks,
-- 
Brice Figureau <brice+apache@xxxxxxxxxxxxxxxx>


---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@xxxxxxxxxxxxxxxx
   "   from the digest: users-digest-unsubscribe@xxxxxxxxxxxxxxxx
For additional commands, e-mail: users-help@xxxxxxxxxxxxxxxx


[Index of Archives]     [Open SSH Users]     [Linux ACPI]     [Linux Kernel]     [Linux Laptop]     [Kernel Newbies]     [Security]     [Netfilter]     [Bugtraq]     [Squid]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Samba]     [Video 4 Linux]     [Device Mapper]

  Powered by Linux