You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@httpd.apache.org by Brice Figureau <br...@daysofwonder.com> on 2007/03/29 12:26:55 UTC

[users@httpd] Complex mod_rewrite URL rewriting - help needed

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@domain.com
  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 <br...@daysofwonder.com>


---------------------------------------------------------------------
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@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


RE: [users@httpd] Relative path with virtual host

Posted by Peter Nyamukusa <pe...@africaonline.co.sz>.



Hi Ken,

Depending on what OS you are running you would need something similar to
this on unix style OS in httpd.conf or vhosts.conf. You would also need to
configure the associated DNS entries for www.myApp.example.com namely the A
record and the www record.

# Use name-based virtual hosting.
#
NameVirtualHost 10.10.10.10:80

#
# myApp
#
<VirtualHost 10.10.10.10:80>
    ServerAdmin webmaster@example.com
    ServerName www.myApp.example.com
    ServerAlias myApp.example.com
    DocumentRoot /var/www/html/myApp
    ErrorLog /var/log/apache2/myApp.example.com/error_log
    CustomLog /var/log/apache2/myApp.example.com/access_log common
</VirtualHost>
#

Hope this helps

------------------------------------
Peter Nyamukusa
Email: petern@africaonline.co.sz
AIM: petenya
 
-----Original Message-----
From: Ken Loomis [mailto:kloomis@it-resources.com] 
Sent: Thursday, March 29, 2007 2:00 PM
To: users@httpd.apache.org
Subject: [users@httpd] Relative path with virtual host

Hello:

This has been bugging me for some time.

If I have a directory structure like this:

/var/www/html/myApp
/var/www/html/myFiles

With this URL:  www.example.com/myApp
I can reference myFiles as ../myFiles

but with this URL: www.myApp.example.com, using a virtual host,
I cannot access myFiles with ../myFiles

Why is this, and can safely I fix this with some directive in the 
Virtual Host? I have a number of libraries I'd like to share across 
several applications.

Any help is much appreciated,

Ken









---------------------------------------------------------------------
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@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org



---------------------------------------------------------------------
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@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


[users@httpd] Relative path with virtual host

Posted by Ken Loomis <kl...@it-resources.com>.
Hello:

This has been bugging me for some time.

If I have a directory structure like this:

/var/www/html/myApp
/var/www/html/myFiles

With this URL:  www.example.com/myApp
I can reference myFiles as ../myFiles

but with this URL: www.myApp.example.com, using a virtual host,
I cannot access myFiles with ../myFiles

Why is this, and can safely I fix this with some directive in the 
Virtual Host? I have a number of libraries I'd like to share across 
several applications.

Any help is much appreciated,

Ken









---------------------------------------------------------------------
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@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org