Forum Moderators: phranque

Message Too Old, No Replies

Many problems with rewrite and alias

         

Haruka

5:23 pm on May 12, 2011 (gmt 0)

10+ Year Member



Hello,

I'm quite newbie with Apache and need some help with URL rewritting and alias.

I'm trying to redirect all my URLs which are inside a /folder/ to the same URI without this /folder/.

For example:

www.example.com/folder/index
should be: www.example.com/index

So I first tried to rewrite the URL using the following code:

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^www\.example\.com\/folder(.*) http://www.example.com/$1 [R=301,L]

So: Anything that starts with an "www.example.com/folder" followed by "anything" should be retypped to an URL like "www.example.com/anything"

I've put this in a .htaccess file inside this /folder/.

I'm not sure if it's a problem with the code itself or if I should put it on the private_html folder.

I've tried a rewrite match too, how you can see:

RedirectMatch 301 ^www\.example\.com\/folder(.*) http://www.example.com/$1

But it also didn't work...I've tried some variations without the "^", without thoose "\" before de dots and nothing worked.

When I open the www.example.com/folder, it doesn't show up any error but just page as it is (in all of my tests). Is there something wrong? Also, I think my Apache settings are okay because there's a .htaccess file in the public_html which redirects all the non-www URLs to the respectives with the www and it works perfectly. I don't know why this code works and my other doesn't. The code is:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

And yes I've tried the folder code with this structure which contains RewriteCond and RewriteRule and it didn't work too.

I've tried to replace the public_html .htaccess with the code that should redirect my folder files, but it gives me an Internal Server Error.

I don't know what is wrong. I've read all the specifications for mod_alias and mod_rewritting and it should be okay. I think it can be an error with my regular expressions and I didn't see anywhere saying if I can use thoose rewrites in the same host. Can someone give me some help?

PS: I'm very sorry about my English. I know it may be a little difficult to understand, but I'd be very happy if someone had the pattience to help me (:

g1smd

7:28 pm on May 12, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Your first attempt was fairly close to being right.

RewriteRule pattern matching can see only the path part of the requested URL.

It cannot see the protocol, domain name, port number or appended query string data.

Haruka

8:23 pm on May 12, 2011 (gmt 0)

10+ Year Member



Are you talking about the RedirectMatch, right?
I hope it has nothing to do with the RewriteRule I used on the non-www rewritting...

But then, how can I use the RedirectMatch in this case? Or I can't use it? By "apprended query string data", you mean I cannot do it, for example:

RedirectMatch 301 ^(.*)/folder(.*)$ http://www.example.com/$1

Because the "/folder" isn't anything but a string...
Is that so? Or am I misunderstanding?

g1smd

8:43 pm on May 12, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



No. I was talking about your first RewriteRule example, not the later RedirectMatch code.


RewriteRule ^folder/(.*) http://www.example.com/$1 [R=301,L]


The RewriteRule pattern matching sees only the path part of the requested URL.

phranque

9:33 pm on May 12, 2011 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



i would strongly recommend against mixing mod_alias and mod_rewrite directives in the same .htaccess, especially if there are any execution-order dependencies.

Haruka

12:09 am on May 13, 2011 (gmt 0)

10+ Year Member



Oh, I think I understood, thx!
I've applied some corrections on my code and made something like this:

Options +FollowSymLinks
RewriteEngine on

RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteRule ^ch/(.*) http://www.example.com/$1 [R=301,L]

And it worked! The best thing is that I didn't need to use the mod_alias here.

g1smd

12:12 am on May 13, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Your www-fixing code does not redirect requests for www.example.com:80/ URLs.
Change the pattern to !^(www\.example\.com)?$ here.

Escape literal periods in patterns.

Your code will create a double redirect for a non-www URL. The non-www to www rule MUST be listed last.

Haruka

2:11 pm on May 13, 2011 (gmt 0)

10+ Year Member



Hm, so I need to have 2 RewriteCond?
I've tried the code bellow and it worked:

RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$ [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteRule ^ch/(.*) http://www.example.com/$1 [R=301,L]

I didn't understand if it's better for me to create two RewriteCond or just push the first rule to the bottom, like:

RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$ [NC]
RewriteRule ^ch/(.*) http://www.example.com/$1 [R=301,L]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

Or I didn't have to change de RewriteCond to begin with? I'm not sure if I have to apply that modification on the Rule or on the Cond ):

g1smd

6:13 pm on May 13, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The CH rule goes first.

The WWW rule with its attached condition goes last.

Always put a blank line after each RewriteRule so you can see what belongs to what.