matching only the first part of REQUEST_URI

Discuss practical ways rearrange URLs using mod_rewrite.

matching only the first part of REQUEST_URI

Postby Cyric » Tue Apr 14, 2009 3:58 am

hi to all,

i have a problem with a rewrite rule and i can't find a way to solve it..
i need to match the requests with an upper case letter in the first part of the request_uri and to rewrite only this part with lowercase letters

example:
http://www.mydomain.com/dir1/file1.html -> no rewrite
http://www.mydomain.com/dir1/File1.html -> no rewrite
http://www.mydomain.com/diR1/file1.html -> http://www.mydomain.com/dir1/file1.html
http://www.mydomain.com/diR1/fiLe1.html -> http://www.mydomain.com/dir1/fiLe1.html

this is the rule i "think" should work.. but it doesn't:

RewriteMap lc int:tolower
RewriteCond %{REQUEST_URI} ^/([A-Z])/(.*)
RewriteRule (.*) /${lc:$1}/$2 [R=301]

thanks in advance :wink:
Cyric
 
Posts: 3
Joined: Tue Apr 14, 2009 2:17 am

Postby richardk » Tue Apr 14, 2009 7:24 am

Code: Select all
^/([A-Z])/

only matches /upper-case letter/. You need /*upper-case letter*/.

Where are you putting the mod_rewrite?

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteMap lc int:tolower

RewriteRule ^/([^/]*[A-Z][^/]*)(/(.*))?$ /${lc:$1}/$3 [R=301,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby Cyric » Tue Apr 14, 2009 11:09 pm

oh.. you are right..

if i understand what you wrote..
^/([^/]*[A-Z][^/]*) this is the first part (what will be lower cased), match if only one letter is upper case and put in $1 all letters between the "/".
(/(.*))?$ this is the last part of the url.. but why is $3 and not $2?

anyway your rewrite seems to work right ;)

thanks

ps. the mod_rewrite is enabled inside a virtualhost directive
Cyric
 
Posts: 3
Joined: Tue Apr 14, 2009 2:17 am

Postby richardk » Wed Apr 15, 2009 9:16 am

(/(.*))?$ this is the last part of the url.. but why is $3 and not $2?

$2 has the slash, $3 does not. The slash is forced (/$3) to save a redirect by mod_dir to add it. (Eg. /Abc to /abc/ instead of /Abc to /abc to /abc/).
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby Cyric » Wed Apr 15, 2009 10:35 pm

i have understand, thank you for your explanation, and for the rewriting rule :wink:
Cyric
 
Posts: 3
Joined: Tue Apr 14, 2009 2:17 am


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: Google [Bot] and 29 guests

cron