Forum Moderators: phranque

Message Too Old, No Replies

Syntaxe Problems with module rewrite of apache

         

Jean_DURAIN

7:44 pm on Feb 4, 2012 (gmt 0)

10+ Year Member



Hello everybody , I discovered the power of .htaccess file, but still can't create right rule.
I need to redirect all none 3w pages of my website to 3w, except some specific url that I want to be reached with 3w or without it.
If any one have an idea please helpe me, I'm facing this problem for two days.
Thank's

g1smd

9:20 pm on Feb 4, 2012 (gmt 0)

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



Let's see your code. You'll use two RewriteCond lines, one testing REQUEST_URI and one testing HTTP_HOST and a RewriteRule to do the redirecting.

There's several thousand prior examples to work from in this forum.

Jean_DURAIN

9:31 pm on Feb 4, 2012 (gmt 0)

10+ Year Member



Thank's for reply, this is my code :
RewriteEngine On
# Test if the url contains one of these exclude1, exclude1, or exclude3

RewriteRule ^(([^/]+/)*)exclude1/$ exclude1$1 [L]
RewriteRule ^(([^/]+/)*)exclude2/$ exclude2$1 [L]
RewriteRule ^(([^/]+/)*)page3/$ exclude3$1 [L]

RewriteCond %{HTTP_HOST} ^domaine.com

RewriteRule (.*) [domaine.com...] [R=301,L]

but, in-fortunately it's not working !

lucy24

9:59 pm on Feb 4, 2012 (gmt 0)

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



Now you need to explain what you mean by "not working". It is obvious to you, but not to us. Does it not redirect at all? Does it redirect files that are not supposed to be redirected? Does it go into an infinite loop?

There are many ways for a rewrite to "not work". There are also many ways for a rewrite to seem to work. But only one of those ways will be absolutely right. When you test it, don't only test inputs that are supposed to work. Also test inputs that are not supposed to work. And test it on garbage input.

For starters...

In your RewriteCond, don't say "if the host is..." such-and-such. Say "if the host is not..." such-and-such.

RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$

I keep getting this wrong, so I gave up and copied from another thread. Memo to self: All punctuation goes outside the parentheses.

Excluding files by using [L] is one way to do it. Another way is to list them in a Condition as

%{REQUEST_URI} !(excludethis|excludethat|excludetheother)

But I'm not sure what you are getting at with this group:

RewriteRule ^(([^/]+/)*)exclude1/$ exclude1$1 [L]

You're not simply skipping over certain files; you're rewriting them in a different way. The Rule seems to say "any request for a directory called 'exclude1' -- which might be nested inside other directories -- gets rewritten to a backward form". For example a request for

www.example.com/dir1/dir2/exclude1/

would serve content from

www.example.com/exclude1dir1/dir2/

Note the missing / in the middle. That's from your rule. Is it intentional?

Jean_DURAIN

10:29 pm on Feb 4, 2012 (gmt 0)

10+ Year Member



Thank you lucy24 for these details, I mean by doesn't work is that it go into an infinite loop.
What I wanted my server to do is redirect all pages in 3w, except pages (or urls containing some keywords).
Example:
[domaine.com...] ===> [domaine.com...]
[domaine.com...] ===> [domaine.com...]
But I need some pages to be reached with 3w and also without 3w.
Example: (url containing "key1" or "key2")
[domain.com...] should not redirected to 3w
[domain.com...] should not redirected to 3w

I set the flag to [L] to stop apache to not execute the next rule( add 3w in the begining of the url) if he finds the excluded keywords in the url.
I wish I explained as it should .
Thank's for your time and your attention

Jean_DURAIN

10:31 pm on Feb 4, 2012 (gmt 0)

10+ Year Member



Thank you lucy24 for these details, I mean by doesn't work is that it go into an infinite loop.
What I wanted my server to do is redirect all pages in 3w, except pages (or urls containing some keywords).
Example:
domaine.com ===> [domaine.com...]
domaine.com/.../.../... ===> [domaine.com...]
But I need some pages to be reached with 3w and also without 3w.
Example: (url containing "key1" or "key2")
domain.com/.../.../..._key1_.../.../... should not redirected to 3w
domain.com/.../.../..._key2_.../.../... should not redirected to 3w

I set the flag to [L] to stop apache to not execute the next rule( add 3w in the begining of the url) if he finds the excluded keywords in the url.
I wish I explained as it should .
Thank's for your time and your attention

I removed the scheme (http) to see the examples

lucy24

11:55 pm on Feb 4, 2012 (gmt 0)

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



If you read the Forums Charter, you will find a rule telling you to use "example.com" for all examples. There are several reasons for the rule. In the Apache forum, the important reason is: example.com (or .net or .org) is not auto-converted into a clickable link. We need to see what you typed, not go to the site.

I set the flag to [L] to stop apache to not execute the next rule( add 3w in the begining of the url) if he finds the excluded keywords in the url.

Yes, that's what I thought. But if your only reason for the [L] is to prevent certain URLs from reaching the www redirect, then the rules should be expressed as

RewriteRule (exception|otherexception|thirdexception) - [L]

meaning "don't do anything, just stop mod_rewrite here".

Next problem: your Rules as written have / at the end. This means they may or may not work for directories, depending on whether mod_dir has already executed. (If it has, then any request for a directory will reach mod_rewrite with "index.html" at the end.) And it will never work for files.

Are your exceptions all directory names, or are some of them filenames? If some of the exceptions are filenames, do they have extensions?

Is there any place where an excepted name could be contained within a non-excepted name? For example: You want to make an exception for /foo/ or /foo.html but not for /foobar/ or /oldfoo.html. Then you have to write the rule and/or conditions very carefully. If you are certain this will never happen, you can -- ahem!-- cheat by ignoring the possibility.

But I need some pages to be reached with 3w and also without 3w.

I am surprised g1 has not said something about this ;)

Why do you want certain pages to have more than one URL? Redirecting in htaccess will not prevent the pages from being reached. Pick one form -- either with or without www -- and use it everywhere.

Jean_DURAIN

12:54 am on Feb 5, 2012 (gmt 0)

10+ Year Member



Thank you Lucy24 for this explanation, this is my first post here so I wasn't aware of this rule( use example.com).
If I understood your explanation, my final rule should be like this:

RewriteEngine On


RewriteRule (exception|otherexception|thirdexception) - [L]

RewriteCond %{HTTP_HOST} ^example.com

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


This shouldn't redirect any url containing exception.
Example: example.com/.../.../..._exception.../.../... will not be redirected to www.example.com/.../.../..._exception.../.../...

lucy24

2:27 am on Feb 5, 2012 (gmt 0)

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



Yes, if you want to do it that way. But I tend to think it would be safer to put it in a Condition attached to the same rule:

RewriteCond %{REQUEST_URI} !(exception|otherexception|thirdexception)

In general, put Redirects before Rewrites (and anything flagged [F] or [G] before both). There are exceptions, and this may be one of them.

When you have more than one Condition, list them from "most likely to fail" to "least likely to fail". If one condition is not met, mod_rewrite does not even have to look at the rest.* (I looked this up. It's way at the bottom of a subsidiary page. Luckily it makes sense intuitively.)


* Exactly like the joke about the three logicians, down in the Foo forum. This is not a coincidence.

g1smd

5:32 am on Feb 5, 2012 (gmt 0)

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



In my earlier post I suggested the outline of what you should do. To add more detail, what you need is:

If the requested domain HTTP_HOST is not exactly the canonical version AND
If the requested path REQUEST_URI is not one of the exclusions
Redirect "everything" (.*) to the www version of the site.


That can be coded using two RewriteConds and one RewriteRule.

Jean_DURAIN

4:43 pm on Feb 5, 2012 (gmt 0)

10+ Year Member



Thank's Lucy24, and Thank you g1smd.
Your answers were very helpful for me, you really saved my day (^_^)