Jump to content

Small Htaccess Learning Question: Rewriterule, Regular expression, Php site


Cluadia

Recommended Posts

Hiya!I'm trying to learn how to write .htaccess. I hate asking for help, but man, google searches have been mean in helping me figure it out. White flag, here I am!

RewriteCond %{SCRIPT_FILENAME} !-d  RewriteCond %{SCRIPT_FILENAME} !-f  RewriteRule ^directory/([a-zA-Z0-9-_;:=]*)/([a-zA-Z0-9-_;:=]*) testpage.php?variableone=$1&variabletwo=$2&variablethree=$3&variablefour=$4 [NC]

Forgive me if the code is horrible, I'm doing my best to figure out what it all means :)Goal:My goal here is making an htaccess file which can pass optional variables, each separated by /, and returning blank variables for those not specified: website.com/variable1/variable2/variable3 for instanceAnd have it be flexible: there doesn't need to be 4 variables, and there doesn't need to be /s for it to work :)Working Goal Examples:website.com/profile is really testpage.php?variableone=profile&variabletwo=&variablethree=website.com/profile/awesomeusername is really testpage.php?variableone=profile&variabletwo=awesomeusername&variablethree= Problem:The problem I'm running into is that the code I have there requires there to be a / after the first variable, or else I am seeing an error page. If I take out the / between the variables, I only get the first variable. Thanks!I would humbly revel in any help on this matter, thanks a ton people! Whether its in the form of direct help, forwarding me to a help file, or just reading it but eventually deciding you'd rather do something more fun than type a response. Thanks people, have a good day :)

Link to comment
Share on other sites

You are not making the forward-slashes optional. If you want to make variable2 optional, then you must also make the slash before variable two optional. I'd group the variables like this to make the second directory optional:

RewriteRule ^directory/([a-zA-Z0-9-_;:=]*)(/([a-zA-Z0-9-_;:=]*))? testpage.php?variableone=$1&variabletwo=$3 [NC]

Link to comment
Share on other sites

RewriteRule ^directory/([a-zA-Z0-9-_;:=]*)(/([a-zA-Z0-9-_;:=]*))? testpage.php?variableone=$1&variabletwo=$3 [NC]

Thanks so much Fmdpa! You rule so much! Coding hero of the month! Im amazed that all it really took was a stroke of genius, rather than learning something that was not already in my knowledge bank, hehe.From your code, I turned it into
^([a-zA-Z0-9-_;:=]*)(/([a-zA-Z0-9-_;:=]*))(/([a-zA-Z0-9-_;:=]*))(/([a-zA-Z0-9-_;:=]*)) testpage.php?variableone=$1&variabletwo=$3&variablethree=$5&variablefour=$7

Everything seems to work great :D Thanks again!

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...