Mod rewrite making me crazy (help please)

Discuss practical ways rearrange URLs using mod_rewrite.

Mod rewrite making me crazy (help please)

Postby maxed2 » Thu Oct 16, 2008 4:19 pm

Okay so im trying to redirect from

http://domain.com/ to http://www.domain.com/
http://domain.com/name1 to http://www.domain.com/name1
so that all non-www urls go to www urls.

also im trying to redirect from
http://www.domain.com/name1/ to http://www.domain.com/name1
http://domain.com/name1/ to http://www.domain.com/name1 (no www and unwanted

trailing slash)
so that unwanted trailing slashes on these variables are 301 redirected to

urls without the slash.

Additionally i wanted to organize my site so that categories are redirected

to:
http://www.doimain.com/+london to http://www.doimain.com/index.php?r=london
http://www.doimain.com/+london+restaurant to

http://www.doimain.com/index.php?r=london&c=restaurant
http://domain.com/name1 to http://www.doimain.com/index.php?d=name1

After alot of googling i came up with the rewrite code in my .htacces:
------------------------------------------------------------------------
Options +FollowSymLinks
RewriteEngine On


# Redirect from non www to www version of domain
RewriteCond %{HTTP_HOST} ^domain.com
RewriteRule (.*) http://www.domain.com/$1 [R=301,NC]

# Redirect to remove trailing slashes on extensionless URL requests
# If requested URL ending with slash does not resolve to an existing directory
RewriteCond %{REQUEST_FILENAME} !-d
# Externally redirect to remove trailing slash
RewriteRule ^(.+)/$ http://www.domain.com/$1 [R=301,NC]

# Redirect pages
RewriteRule ^+([a-z0-9]+)+([a-z0-9]+)$ index.php?r=$1&c=$2 [NC,L]
RewriteRule ^+([a-z]+)$ index.php?a=$1 [NC,L]
RewriteRule ^([a-z0-9]+)$ index.php?d=$1 [NC,L]
-----------------------------------------------------------------------------

But it dosnt seem to work :(
help please!!

Thanks
max
maxed2
 
Posts: 7
Joined: Thu Oct 16, 2008 4:16 pm

Postby maxed2 » Thu Oct 16, 2008 7:49 pm

got it working using
--
Options +FollowSymLinks
RewriteEngine On

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

#get rid of trailing slashes
RewriteCond %{HTTP_HOST} ^(www.)?example\.com$ [NC]
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]

#page redirect
RewriteRule ^\+([a-zA-Z]+)$ index.php?r=$1 [L]
--
maxed2
 
Posts: 7
Joined: Thu Oct 16, 2008 4:16 pm

Postby richardk » Fri Oct 17, 2008 9:06 am

Code: Select all
Options +FollowSymLinks

RewriteEngine On

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

RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
# Don't remove slashes from existing directories.
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R=301,L]

# Page redirect
RewriteRule ^\+([a-z]+)$ /index.php?r=$1 [NC,QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby maxed2 » Sun Oct 19, 2008 12:39 am

thanks richard ive nearly got everything working now, with some help i was able to get to this stage where everything works, except one rule:

Code: Select all
Options +FollowSymLinks
RewriteEngine On
#
# Externally redirect to get rid of trailing slashes except for home page
RewriteRule ^(.+)/$ http://www.example.com/$1 [R=301,L]
#
# Externally redirect *only* direct client requests for the script back to friendly URLs
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?r=([a-z]+)
RewriteRule ^index\.php$ http://www.example.com/in-%1? [R=301,L]
#
# Externally redirect to force canonical hostname
RewriteCond %{REQUEST_URI} ^[-/0-9a-z]+$
RewriteCond %{HTTP_HOST} ^example\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com(\.|\.?:[0-9]+)$ [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
#
# Internally rewrite "friendly" URL requests to index.php
RewriteRule ^in-([a-z]+)$ index.php?r=$1 [NC,L]


and on the begining of my index.php i have

Code: Select all
<?
$str =$_SERVER["REQUEST_URI"];
$str = strtolower($str);
$strt=strcmp($str,$_SERVER["REQUEST_URI"]);
if (!$strt==0)
{
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.example".$str);
header("connection: close");
exit();
}
?>


Basically the "RewriteCond %{REQUEST_URI} ^[.-/0-9a-z]+$" was supposed to only let the url redirect to fix non-www url's take place if the URI contained no uppercase characters, which would let the php script perform the necessary changes and perform a redirect with the fixed lowercase string appended.....

however the ""RewriteCond %{REQUEST_URI} ^[.-/0-9a-z]+$"" seems not to work as intended

it seems that
http://example.com/in-name is redirected to
http://www.example.com/index.php?r=name (*shock*) which then redirects to http://www.example.com/in-name .

if I remove the "." from the ^[.-/0-9a-z]+$ then everything seems fine but then any uri that contains a "." is simply ignored...
maxed2
 
Posts: 7
Joined: Thu Oct 16, 2008 4:16 pm

Postby richardk » Sun Oct 19, 2008 11:06 am

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

# Remove trailing slashes from non-directories.
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.+)/$ http://www.example.com/$1 [R=301,L]

# Redirect /index.php?r=* to /in-*
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^(.*&)?r=([^&]+)(&.*)?$ [NC]
RewriteRule ^(index\.php)?$ http://www.example.com/in-%2? [R=301,L]

# Add www to all URLs that do no include uppercase characters (![A-Z]).
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ![A-Z] http://www.example.com%{REQUEST_URI} [R=301,L]

# Rewrite /in-* to /index.php?r=*
RewriteRule ^in-([a-z]+)$ /index.php?r=$1 [NC,QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby maxed2 » Sun Oct 19, 2008 12:41 pm

thanks for the reply, just before i try out what you have suggested my host has allowed me to add some rules to the httpd.conf for my site, so i was thinking it would be better to do that, ive told him to add the rules:

Code: Select all
RewriteEngine On
RewriteMap lc int:tolower
RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule ^([^/]+)/?$ http://www.example.com/{lc:$1} [R=301,L]


I thought it was supposed to perform any redirection only with lowered case letters and if there is a slash remove it.am i right?
maxed2
 
Posts: 7
Joined: Thu Oct 16, 2008 4:16 pm

Postby maxed2 » Sun Oct 19, 2008 2:20 pm

actually it may not be wise to remove slashes from every request as some may be real physical directories so i am a bit stuck at the moment, i guess two redirects will have to be called if a request for a physical directory containing a slash at the end (http://example.com/in-NAME/) is requested....

my httpd.conf i am going to use:
Code: Select all
RewriteEngine on
RewriteMap lc int:tolower


and in my .htacces im going to call the defined rewritemap and add:

RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule ^(.+)$ http://www.example.com/${lc:$1} [R=301,L]

this works fine, but what about when someone requests:
http://www.example.com/variable/
this is not desirable since i have a rule also added to remove trailing slashes from non-directory uri's.

So how can i modify this rule so that it lowercases and removes a trailing slash from a non-physical-directory and redirects..?
maxed2
 
Posts: 7
Joined: Thu Oct 16, 2008 4:16 pm

Postby maxed2 » Sun Oct 19, 2008 11:27 pm

Okay everything works now urls containing uppercase letters are redirected with all lowercase letters , the only problem is with trailing slashes.

There seems to be no way i can say from all uri's that contain uppercase letters and a trailing slash redirect to a url with the uri converted to lowercase and the trailing slash removed, except if a physical directory is requested, in which case leave the trailing slash and just redirect to a url containing the lowercased uri.

At the moment if a uri containing a trailing slash with any uppercase letters is requested, then there is redirection chain that first redirects to convert everything to lowercase then remove the slash.....

My httpd.conf contains:
--
RewriteEngine On
RewriteMap lc int:tolower
--

My .htaccess contains:
--
Options +FollowSymLinks
RewriteEngine On
#redirects to lowercased uri
RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule ^(.+)$ http://www.example.com/${lc:$1} [R=301,L]
#
# Externally redirect to get rid of trailing slashes except for home page
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.+)/$ http://www.example.com/$1 [R=301,L]
#
# Externally redirect *only* direct client requests for the script back to friendly URLs
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?r=([a-zA-Z0-9]+)
RewriteRule ^index\.php$ http://www.example.com/in-${lc:%1}? [R=301,L]
#
# Externally redirect to force canonical hostname
RewriteCond %{HTTP_HOST} ^example\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com(\.|\.?:[0-9]+)$ [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
#
# Internally rewrite "friendly" URL requests to index.php
RewriteRule ^in-([a-z]+)$ index.php?r=$1 [L]
--

There must be some way to check if the lowercased uri that is genereated is requesting a physically existing directory, if so leave and simply redirect else remove the slash and then redirect.....

argh mod_rewrite kills me!
maxed2
 
Posts: 7
Joined: Thu Oct 16, 2008 4:16 pm

Postby richardk » Mon Oct 20, 2008 11:48 am

Use the lowercase RewriteMap on all trailing slash redirects
Code: Select all
Options +FollowSymLinks

RewriteEngine On

# Remove trailing slashes from non-directories.
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.+)/$ http://www.example.com/${lc:$1} [R=301,L]

# Redirect /index.php?r=* to /in-*
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^(.*&)?r=([^&]+)(&.*)?$ [NC]
RewriteRule ^(index\.php)?$ http://www.example.com/in-${lc:%2}? [R=301,L]

# Remove any uppercase characters.
RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule ^(.+)$ http://www.example.com/${lc:$1} [R=301,L]

# Add www to all URLs that do no include uppercase characters (![A-Z]).
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ![A-Z] http://www.example.com%{REQUEST_URI} [R=301,L]

# Rewrite /in-* to /index.php?r=*
RewriteRule ^in-([a-z]+)$ /index.php?r=$1 [NC,QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby maxed2 » Mon Oct 20, 2008 1:36 pm

thanks everything works! :)
maxed2
 
Posts: 7
Joined: Thu Oct 16, 2008 4:16 pm


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 6 guests

cron