PDA

View Full Version : .htaccess rewrite rules


dmegatool
2012-08-20, 12:49
Hey guys,
I'm looking to append something on a url. So when acessing mysite.com/folder1, the url is mysite.com/append/folder1. Anybody can help me with the htacess rewrite rule ?

Brad
2012-08-20, 15:14
Do you want an external redirect or an internal rewrite?

Redirect (HTTP 301 or 302 response):
- user accesses mysite.com/folder1
- apache tells browser to go to mysite.com/append/folder1
- browser loads (and user sees the URL change to) mysite.com/append/folder1

Rewrite:
- user accesses mysite.com/folder1
- apache instead serves content as if mysite.com/append/folder1 was the requested URL
- the user only ever sees the URL that he entered

dmegatool
2012-08-20, 19:55
The content would be in mysite.com/folder1 but the URL shown in the bar has to be mysite.com/append/folder1 so I guess we're talking rewrite.

I'm just fooling around to see if I can bypass a situation that happen with my Drupal installation. I got a main site with a subsite within with a different db and all. They're not connected in any way. It works if I just put the subsite folder inside my main installation. If I access mysite.com/folder1, it loads the subsite as expected.

Problem is I wanted to keep a certain folder structure. In the main site, I got urls like mysite.com/append/ & mysite.com/append/folder666. So I can't just put my folder1 inside a /append folder cause obviously, mysite.com/append/ will load that empty folder.

What I'm trying is setting up an url alias in my main Drupal install so mysite.com/append/folder1 actually points to mysite.com/folder1 and then altering the url shown... Don't even know if that makes sense. I don't really know how apache handles the rewrites.

Sorry for the not so clear explanation :)

Brad
2012-08-20, 22:04
Without knowing more about the guts of your setup, I think it should look something like this:

RewriteEngine On
RewriteRule ^folder1/?$ append/folder1 [NC,L]


This pattern matches exactly "folder1" and "fodler1/" and will internally rewrite the request to "append/folder1".

Some explanations:

"^folder1/?$" is the URL pattern the rule is checking against. There are several special characters here. ^ means the beginning of the URL and $ means the end of the URL. The ? is the "optional preceding character" marker, and when used after the /, it means that the / is optional and that the pattern will match whether or not the / is there.

"[NC,L]" is a set of mod_rewrite flags. NC means to treat the pattern match as case insensitive (so "FoLdEr1" would also match and be rewritten); if you want to preserve case then throw that out. L means that when this rule is triggered, stop trying to process other rules against this requested URL. You probably want to keep that.

If you have several other paths you want to rewrite and they match a pattern like "folder1" and "folder666", you could change the rule to look like this:

RewriteEngine On
RewriteRule ^folder([0-9]+)/?$ append/folder$1 [NC,L]


That would rewrite any request to "folder" with some numbers after it to "append/folder" with the same numbers after it. In this example, "[0-9]+" is a regular expression that says "match if there is at least one number here". Putting that expression in parentheses tells mod_rewrite to "remember" this group and allow it to be used in the rewritten URL. In the rewritten URL, it gets dropped where you see "$1". mod_rewrite simply numbers groups in the order it encounters them. So, if you had more groups, they would be $2, $3, and so on.

What if you have files under that folder that you want to redirect too? In that case, you might just want to glob *everything* after "folder" into the URL like this:

RewriteEngine On
RewriteRule ^folder(.*)$ append/folder$1 [NC,L]


In this case, ".*" is a regular expression that says "match everything even if there's nothing left to match". So, this would match "folder", "FoLDeR", "folder1", "folderFOO", "folder/foo/bar", and so on.

dmegatool
2012-08-20, 23:15
Thx man. I'll check this out tomorrow :)