Forum Moderators: phranque

Message Too Old, No Replies

htaccess conditional on url

         

billuk

3:58 pm on Sep 1, 2008 (gmt 0)

10+ Year Member



Hi, I have 2 different websites using the same social network via a symbolic link. The problem is that the directory names are different on each website (because of language). This means that I need a different rewrite rule based on the request url.

Is there a simple way to do this in a .htaccess file:

IF REQUEST_URL = WWW.EXAMPLE.COM THEN
{
RewriteEngine On
Options +Followsymlinks
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.* - [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*/images/(.*)$ /community/images/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*/uploads_user/(.*)$ /community/uploads_user/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ /community/profile.php?user=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/albums/([0-9]+)/([0-9]+)/?$ /community/album_file.php?user=$1&album_id=$2&media_id=$3 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/albums/([0-9]+)/?$ /community/album.php?user=$1&album_id=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/albums/([0-9]+)/([^/]+)?$ /community/album.php?user=$1&album_id=$2$3 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/albums/?$ /community/albums.php?user=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/classifieds/([0-9]+)/?$ /community/classified.php?user=$1&classified_id=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/classifieds/([0-9]+)/([^/]+)?$ /community/classified.php?user=$1&classified_id=$2$3 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/classifieds/?$ /community/classifieds.php?user=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/blog/([0-9]+)/?$ /community/blog_entry.php?user=$1&blogentry_id=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/blog/([^/]+)?$ /community/blog.php?user=$1$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/blog/?$ /community/blog.php?user=$1 [L]

}

ELSEIF REQUEST_URL = WWW.EXAMPLE.ES THEN

{

RewriteEngine On
Options +Followsymlinks
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.* - [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*/images/(.*)$ /comunidad/images/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*/uploads_user/(.*)$ /comunidad/uploads_user/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ /comunidad/profile.php?user=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/albums/([0-9]+)/([0-9]+)/?$ /comunidad/album_file.php?user=$1&album_id=$2&media_id=$3 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/albums/([0-9]+)/?$ /comunidad/album.php?user=$1&album_id=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/albums/([0-9]+)/([^/]+)?$ /comunidad/album.php?user=$1&album_id=$2$3 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/albums/?$ /comunidad/albums.php?user=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/classifieds/([0-9]+)/?$ /comunidad/classified.php?user=$1&classified_id=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/classifieds/([0-9]+)/([^/]+)?$ /comunidad/classified.php?user=$1&classified_id=$2$3 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/classifieds/?$ /comunidad/classifieds.php?user=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/blog/([0-9]+)/?$ /comunidad/blog_entry.php?user=$1&blogentry_id=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/blog/([^/]+)?$ /comunidad/blog.php?user=$1$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/blog/?$ /comunidad/blog.php?user=$1 [L]

}

Hope that makes sense, (if it helps it's only the directory name that is different, the .php files remain the same).

Help! Thanks in advance.

jdMorgan

6:54 pm on Sep 1, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, the "user-defined variable" feature of mod_rewrite can be used to do this, along with some other redundancy elimination, to significantly shorten and speed up your code.

RewriteEngine On
Options +FollowSymLinks
#
# Skip all following rules if the requested URL resolves to existing directory or file
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .* - [L]
#
# Set requested-hostname/language-based "community" directory name to "community" (default value)
RewriteRule . - [E=ComDir:community]
#
# If Spanish tld requested, set "community" directory name to "comunidad"
RewriteCond %{HTTP_HOST} ^www\.example\.es
RewriteRule . - [E=ComDir:comunidad]
#
RewriteRule /images/(.*)$ /%{ENV:ComDir}/images/$1 [L]
RewriteRule /uploads_user/(.*)$ /%{ENV:ComDir}/uploads_user/$1 [L]
RewriteRule ^([^/]+)/?$ /%{ENV:ComDir}/profile.php?user=$1 [L]
RewriteRule ^([^/]+)/albums/([0-9]+)/([0-9]+)/?$ /%{ENV:ComDir}/album_file.php?user=$1&album_id=$2&media_id=$3 [L]
RewriteRule ^([^/]+)/albums/([0-9]+)/?$ /%{ENV:ComDir}/album.php?user=$1&album_id=$2 [L]
RewriteRule ^([^/]+)/albums/([0-9]+)/([^/]+)?$ /%{ENV:ComDir}/album.php?user=$1&album_id=$2$3 [L]
RewriteRule ^([^/]+)/albums/?$ /%{ENV:ComDir}/albums.php?user=$1 [L]
RewriteRule ^([^/]+)/classifieds/([0-9]+)/?$ /%{ENV:ComDir}/classified.php?user=$1&classified_id=$2 [L]
RewriteRule ^([^/]+)/classifieds/([0-9]+)/([^/]+)?$ /%{ENV:ComDir}/classified.php?user=$1&classified_id=$2$3 [L]
RewriteRule ^([^/]+)/classifieds/?$ /%{ENV:ComDir}/classifieds.php?user=$1 [L]
RewriteRule ^([^/]+)/blog/([0-9]+)/?$ /%{ENV:ComDir}/blog_entry.php?user=$1&blogentry_id=$2 [L]
RewriteRule ^([^/]+)/blog/([^/]+)?$ /%{ENV:ComDir}/blog.php?user=$1$2 [L]
RewriteRule ^([^/]+)/blog/?$ /%{ENV:ComDir}/blog.php?user=$1 [L]

Note: Unnecessary directives, anchors, and rules were intentionally removed; This should be all you need.

Jim

jdMorgan

7:04 pm on Sep 1, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Note that I strongly recommend that you add domain canonicalization redirect rules ahead of these internal redirects. A quick analysis will reveal that requests for "example.es" (no "www") will result in english content being served. To solve both this and the duplicate-content problem of un-canonicalized domains, add something like this ahead of all of your internal rewrite rules:

# Redirect to canonicalize domain names
#

RewriteCond %{HTTP_HOST} !^www\.example\.es$
RewriteRule (.*) http://www.example.es/$1 [R=301,L]
#
RewriteCond %{HTTP_HOST} !\.es\.?(:[0-9]+)$
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]


Again, the structure here creates a default to ".com" if anything other than the ".es" TLD is requested.

Jim

[edited by: jdMorgan at 7:05 pm (utc) on Sep. 1, 2008]

billuk

11:17 am on Sep 2, 2008 (gmt 0)

10+ Year Member



Hi Jim,

Thanks for your help, it's much appreciated.

It works perfectly, excellent stuff!

The only problem is the canonicalization redirect bit, when I add that it bounces between the two, i.e. .es redirect to .com and vice versa.

Cheers Jim

jdMorgan

4:22 pm on Sep 2, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, a second error on my edit above... See that blank line? It should have been:

# Redirect to canonicalize domain names
#
RewriteCond %{HTTP_HOST} \.es\.?(:[0-9]+)$
RewriteCond %{HTTP_HOST} !^www\.example\.es$
RewriteRule (.*) http://www.example.es/$1 [R=301,L]
#
RewriteCond %{HTTP_HOST} !\.es\.?(:[0-9]+)$
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

Jim

billuk

5:35 pm on Sep 2, 2008 (gmt 0)

10+ Year Member



Thanks Jim, you certainly know your stuff.

Bill