cancel
Showing results for 
Search instead for 
Did you mean: 

Using ModRewrite

Boxersoft
Rising Star
Posts: 132
Thanks: 20
Fixes: 1
Registered: ‎25-07-2009

Using ModRewrite

I have mydomain hosted by PlusNet and DNS is pointing it to the ccgi server. I'm trying to use mod_rewrite in .htaccess to allow http://mydomain.com/ to be rewritten to a corresponding folder under my ccgi root, e.g. ccgi.myaccount.plus.com/mydomain/. I've got it working, but it's redirecting rather than rewriting, i.e. http://ccgi.myaccount.plus.com/mydomain/ is appearing in the browser's address bar. I want http://mydomain.com/ to show. From what I've read about mod_rewrite, this should happen by default, and what I'm getting should only happen if I add to the end of the RewriteRule (which, of course, I'm not doing).
I've had a quick trawl through previous postings in the hope that this would be a FAQ, but I couldn't see anything obviously related. Can anybody suggest what I might be doing wrong?
12 REPLIES 12
Ben_Brown
Grafter
Posts: 2,839
Registered: ‎13-06-2007

Re: Using ModRewrite

Without seeing the rewrite rules you're using it's impossible to tell exactly what you've done wrong. If you post them in here we can take a look.
Boxersoft
Rising Star
Posts: 132
Thanks: 20
Fixes: 1
Registered: ‎25-07-2009

Re: Using ModRewrite

RewriteEngine on
# If the domain is [www.]mydomain[...]
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain [nc]
# ... then re-write the URL to point at the corresponding folder on the CCGI server
RewriteRule ^(.*)$ http://ccgi.myaccount.plus.com/mydomain/$1

The browser shows the expected content, but the address bar shows the plus.com URLs instead of http://mydomain URLs.
Gabe
Grafter
Posts: 767
Registered: ‎29-10-2008

Re: Using ModRewrite

As Ben was about to say, if I hadn't jumped in:
Try something like
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.co\.uk$ [nc]
RewriteCond %{REQUEST_URI} !^/folder/
RewriteRule (.*) /folder/$1

Gabe
Boxersoft
Rising Star
Posts: 132
Thanks: 20
Fixes: 1
Registered: ‎25-07-2009

Re: Using ModRewrite

Excellent - thanks Gabe! I guess supplying an absolute URL was forcing the Redirect?
The code you supplied seems to be working fine with just one wrinkle that I've noticed so far, and that's a difference in behaviour with and without a trailing slash on a folder name. Given inputs of:

  • mydomain/foo/

  • mydomain/foo


... both correctly deliver the content. However, whereas the first (with the trailing slash) shows in the address bar as typed, the second displays as:
mydomain/domainfolder/foo/
... which is how things are organised on the server, but I want it to appear as it does when the trailing slash is supplied.
Any ideas?

Steve.
Ben_Brown
Grafter
Posts: 2,839
Registered: ‎13-06-2007

Re: Using ModRewrite

Give this one a try:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.co\.uk$ [NC]
RewriteCond %{REQUEST_URI} !^/folder/?
RewriteRule (.*) /folder/$1

It's very similar but subtly different.
Boxersoft
Rising Star
Posts: 132
Thanks: 20
Fixes: 1
Registered: ‎25-07-2009

Re: Using ModRewrite

Thanks Ben, but that seems to be behaving in the same way. I wondered if it might be a caching issue but I tried clearing it (in separate browsers) and the result is the same - the unwanted folder name appears when no trailing slash is provided.
Gabe
Grafter
Posts: 767
Registered: ‎29-10-2008

Re: Using ModRewrite

That's the trailing-slash problem, which needs an external redirect. Try adding these lines right after the rewriteengine on
RewriteCond    %{REQUEST_FILENAME}  -d
RewriteRule    ^(.+[^/])$          $1/ 
Gabe
Boxersoft
Rising Star
Posts: 132
Thanks: 20
Fixes: 1
Registered: ‎25-07-2009

Re: Using ModRewrite

That doesn't seem to make any difference, unfortunately - I still end up with the unwanted folder name appearing in the browser's address bar if the supplied URL doesn't have a trailing slash. Whether the supplied URL has a trailing slash or not, the URL that appears in the browser's address bar does have one. I don't know if that helps at all.
Thanks for the input - and, of course, Merry Christmas!

Steve.
Gabe
Grafter
Posts: 767
Registered: ‎29-10-2008

Re: Using ModRewrite

Sorry, slight logical snafu there.
Try:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !^.*/[^/]+\.[^/]+$
RewriteRule ^(.+[^/])$ $1/ [R,L]
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.co\.uk$ [NC]
RewriteCond %{REQUEST_URI} !^/folder/
RewriteRule (.*) /folder/$1

Gabe
Boxersoft
Rising Star
Posts: 132
Thanks: 20
Fixes: 1
Registered: ‎25-07-2009

Re: Using ModRewrite

Excellent, that seems to do the job nicely - thanks very much!
I'm struggling to understand how it works though. The RewriteRule seems unchanged, and if I understand the regex it's just capturing anything without a trailing slash and adding one. So far so good. Unless I've missed something important, the only difference between your two versions is the RewriteCond that limits this rule, changing from    %{REQUEST_FILENAME}  -d to %{REQUEST_URI} !^.*/[^/]+\.[^/]+$. The first one was restricting the rule to directories, I believe, and that seems sensible - but it evidently didn't work as expected. The second seems to match something with a single slash, followed by at least one non-slash, followed by anything and ending in at least one non-slash. I'm not sure if I've got that quite right but, whether I have or not, I don't understand why it's needed or why the first version didn't work.
I'm currently trying to build on your solution by adding some of the nice tricks that I've read are achievable with mod_rewrite, such as /foo/bar -> /foo/BarPage.php or whatever. I rather thought such things would be straightforward once you had got me started, but I'm not finding it that easy at all. Depending on where I insert my rule (and possibly whether or not I use ) I either get the page served correctly but with the unwanted folder name appearing again, or the folder name is absent but the page seems to be served without its stylesheet being used etc. I find myself floundering in my attempts to understand the workings of mod_rewrite and resorting to blind trial and error (and still failing) - it's like being a wet-behind-the-ears novice programmer all over again!
Gabe
Grafter
Posts: 767
Registered: ‎29-10-2008

Re: Using ModRewrite

I was forgetting  Crazy that on the ccgi platform, REQUEST_FILENAME doesn't contain the full filepath, so it's never going to check as a directory. Rather than reconstruct it on the left hand side of the condition, we can just test the uri against a regex, which is probably slightly more efficient.
The reason for the trailing-slash problem is that a directory without a trailing slash is just wrong and mod_dir will try to fix that by redirecting to the canonical url (which contains the directory you don't want to see). The rewrite aims to add the missing slash before mod_dir does it for you and reveals all.
We don't want to add a slash to files, so the condition checks that the REQUEST_URI doesn't (the !) match a pattern starting with any or no string followed by a slash, followed by one or more characters that are not slashes, followed by a dot (it's escaped), followed by one or more characters that are not slashes (like a file extension). It's not a precise pattern, but sufficient to differentiate.
You and me both. There's always something to bite you, usually involving an infinite loop.
Gabe
Boxersoft
Rising Star
Posts: 132
Thanks: 20
Fixes: 1
Registered: ‎25-07-2009

Re: Using ModRewrite

OK, thanks. I've installed Apache here so I can have a poke about locally and have access to the logs.