user2308604
user2308604

Reputation: 5

Using .htaccess for a variable folder

I am having a little trouble with using .htaccess for what I need it to do.

This is the URL of the webpage:

http://example.com/folder/$1/topic.php?id=$2

The only two things that change are $1 and $2.

I was using this code, but I cannot seem to get it to work with the variable folder.

RewriteRule ^([a-zA-Z0-9_-]+)$ topic.php?id=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ topic.php?id=$1
RewriteRule ^folder/$1/([a-zA-Z0-9_-]+)$ folder/$1/topic.php?id=$2
RewriteRule ^folder/$1/([a-zA-Z0-9_-]+)/$ folder/$1/topic.php?id=$2

I may have made some mistake with the syntax but I cannot seem to figure it out.

The browser URL will change to look like:

http://example.com/folder/$1/topic/$2

Thank you for your time ^.^

Upvotes: 0

Views: 113

Answers (1)

Yay! user-sama.

I think you need to escape $1, just give it some backslash ninja punch:

RewriteRule ^folder/\$1/([a-zA-Z0-9_-]+)/$ folder/$1/topic.php?id=$2

Or it might be:

RewriteRule ^folder/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ folder/$1/topic.php?id=$2

^__^

Yeah, i need to explain what's going on, so what's between parentheses in the left becomes $ on the right, the number that comes after $ is the present order of parentheses. So:

#             1st   2nd   3rd        1st────┐    2nd    ┌──────3rd   
RewriteRule ^(.*?)/(.*?)/(.*)$ file.php?p1=$1&p2=$2&p3=$3

These parentheses blocks are called capture block, it's RegExp.

Upvotes: 2

Related Questions