Htaccess and Skip condition

Hi

I have a htaccess file with 2 rewrite rules which have the same rewritecond. It does not seem correct to duplicate the rewritecond although i cannot get the skip function to work.

It would be appreciated if someone can point out what exactly i’m doing wrong as I’ve tried so many different combinations now:


RewriteCond %{HTTP_HOST} !^www\\.example\\.com$
RewriteRule .* - [S=2]
RewriteRule ^/?page1.html$ /404.html?%1 [R=301,L]
RewriteRule ^/?page2.html?$ /404.html?%1 [R=301,L]

Thanks in advance

p_h_p,

Good! Skip is a useful directive! So is the Last flag, though, so I’d add that to the Skip flag.

Also, please remember that %{HTTP_HOST} is case insensitive so the No Case flag is appropriate there.


RewriteCond %{HTTP_HOST} !^www\\.example\\.com$ [COLOR="Blue"][NC][/COLOR]
RewriteRule .* - [S=2[COLOR="Blue"],L[/COLOR]]
RewriteRule ^/?page1.html$ /404.html[COLOR="Red"]?%1[/COLOR] [R=301,L]
RewriteRule ^/?page2.html[COLOR="Magenta"]?[/COLOR]$ /404.html[COLOR="Magenta"]?[/COLOR][COLOR="Red"]%1[/COLOR] [R=301,L]
[COLOR="Red"]#[/COLOR] WHAT are the ?%1 bits? You've NOT created that variable in your RewriteCond statement! 
[COLOR="Red"]#[/COLOR] The effect is to KILL any pre-existing query string (because %1 is NULL)!
[COLOR="Magenta"]#[/COLOR] WHY the ?  Is the 'l' in 'html' optional (as this regex says)?

Regards,

DK

A last flag on a skip flag? :shifty:

Please consider the following:


RewriteRule On
RewriteCond %{HTTP_HOST} ^www\\.example\\.com$
RewriteRule . - [S=1,L]
RewriteRule ^foo/(\\w+)$ /index.php?foo=$1 [L]
RewriteRule ^bar/(\\w+)$ /index.php?bar=$1 [L]

Rewrites for /bar will never fire, because the rewriting process is stopped as soon as the skip-last is fired, thus the rest of the .htaccess will never be processed.
Just tried it on my server, /bar/whatever gives a 404. If I remove the last flag from the skip flag it works.

Rémon,

p_h_p wanted to redirect ONLY if NOT www.example.com, i.e., if www.example.com then run (ignore the ? & %1 errors):

RewriteRule ^/?page1.html$ /404.html?%1 [R=301,L]
RewriteRule ^/?page2.html?$ /404.html?%1 [R=301,L]

Therefore, if NOT www.example.com, then SKIP those two RewriteRules. Check it out for yourself!

Regards,

DK

I know what p_h_p asked and I know what the skip flag does.
What you’re saying has nothing to do with my post. I only said applying a last flag to a skip flag cripples the .htaccess. All I’m saying is don’t apply a last flag to a skip flag, as it produces unwanted results.

Hi

Thanks for the replies.

The skip flag still won’t work. Please find latest update to the code:


RewriteCond %{HTTP_HOST} !^www\\.example\\.com$ [NC]
RewriteRule .* - [S=2, L]
RewriteRule ^/?page1.html$ /404.html [R=301,L]
RewriteRule ^/?page2.html$ /404.html [R=301,L]

I have tried [S=2, L] and [S=2] with no joy.

Your help is much appreciated.

p_h_p,

Please explain in WORDS exactly what you’re trying to do. E.g., if www.example.com, redirect page1.html to 404.html OR redirect page2.html to 404.html otherwise, do nothing. Okay, that’s my interpretation of the CURRENT version of the mod_rewrite code. Personally, I’d make it (Apache 2.x and skip = 1) RewriteRule ^(page1|page2)\.html$ 404.html [R=301,L] where I assume you will substitute real file names for page1 and page2.

BTW, if you merely remove those two pages and use ErrorDocument /404.html, you’ll be farther ahead as you won’t need to load the regex engine to process a core directive like that.

Regards,

DK

Hi dklynn
Thanks for your reply.

What I am looking for is:
if domain is not equal to www.example.com skip the following two rewrite rules. At present the two rewriterules execute which I do not want to happen. I can resolve this by doing the following although I am duplicating the rewritecond which I do not like:


RewriteCond %{HTTP_HOST} !^www\\.example\\.com$ [NC]
RewriteRule ^/?page1.html$ /404.html [R=301,L]
RewriteCond %{HTTP_HOST} !^www\\.example\\.com$ [NC]
RewriteRule ^/?page2.html$ /404.html [R=301,L]

I am doing this in htaccess which could be where the problem lies. Does the skip tag work in htaccess or only in the config files?

Thanks

p_h_p,

What you have in the above post (#6) is just fine. No reason for it not to work as you intended.

Regards,

DK

Thanks for your help.

I have managed to get it to work.