The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> System Administration
> Apache Development
|
Mod_Rewrite for 6 variables
Discuss Mod_Rewrite for 6 variables in the Apache Development forum on Dev Shed. Mod_Rewrite for 6 variables Apache Development forum discussing HTTP Server general topics, configuration, and modules. Apache is an open source web server that runs on multiple platforms.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 26th, 2013, 10:01 PM
|
|
|
|
Mod_Rewrite for 6 variables
I've got six variables in a url that I need to put into a clean url.
Here's the current .htaccess file:
Code:
RewriteEngine on
Options -MultiViews
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)(/)?$ /index.php?RF=$1&loc=$2&c=$3&ex=$4&jc=$5&f0=$6 [R,QSA]
When I type in what I want to be converted:
darkentity-static.info/DARK/ge/si/14/css/style
It ends up at:
http://darkentity-static.info/index.php?RF=ge&loc=si&c=14&ex=css&jc=style&f0=
when it should be:
http://darkentity-static.info/index.php?RF=DARK&loc=ge&c=si&ex=15&jc=css&f0=style
Can't figure out why, for some reason the regex is not martching the "DARK" in the URL.
|

February 26th, 2013, 10:18 PM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
Try changing those *s to +s. If the URL it tries to match starts with a slash then your first [^/]* will match nothing (it's optional after all) and the rest of your groups will be pushed down by one.
But you also don't have enough groups: five there when you need six.
In general you should try to be specific with expressions. Is that "14" always a number? Is "css" always a particular thing, like "css" or "js" or "images"? How about that "style"?
Code:
RewriteRule ^/?([^/]+)/([^/]+)/([^/]+)/(\d+)/([^/]+)/([^/]+)/?$ /index.php?RF=$1&loc=$2&c=$3&ex=$4&jc=$5&f0=$6 [L,QSA]
Also:
- It's good practice to anchor the URL at the beginning and end unless you really don't care where in the URL it matches.
- I use ^/? because in some setups the URL may start with a slash and in others it may not. Your setup looks like it does so you could just keep ^/.
- Your [R] flag will redirect the user to the new URL. You sure you actually want that?
- The [L] flag is another good thing to use. Without it, Apache and mod_rewrite will keep on testing, matching, and replacing according to other patterns you might have. [L] tells it to stop.
|

February 26th, 2013, 10:42 PM
|
|
|
I was using the R flag for testing purposes, to see where it was trying to go. Will remove on the production version.
Yes, the ex variable (in this case with the value of 14) will always be a digit.
The jc variable (in this case with the value of css) will always either be css or js. I guess, in that instance, I could do "css|js" as the match, instead of "^/".
BTW, that rewrite rule you posted works perfectly, thanks.
I modified it to this:
Code:
^/?([^/]+)/([^/]+)/([^/]+)/(\d+)/([css|js]+)/([^/]+)/?$
Seems to be working, but just thought I double check to make sure that's right.
Last edited by HDFilmMaker2112 : February 26th, 2013 at 11:50 PM.
|

February 26th, 2013, 11:08 PM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
Almost. []s are for character sets: any character you put in there will match. Since you put "css|js" with a + it'll match any number of "c", "s", "|" (which loses its special meaning in character sets), and "j" characters. Like "jscjcscjsjcjcss".
Use a simpler
|

February 26th, 2013, 11:48 PM
|
|
|
|
Works excellent. Thanks a ton.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|