301..302

I’ve set up recently some ModRewrites…

RewriteRule ^/?([a-zA-Z_]+)/([a-zA-Z1-9_]+)$ index.php?var1=$1&var2=$2 [L,NC]
RewriteRule ^/?([a-zA-Z_]+)(/([a-zA-Z1-9_]+)(/([0-9]+))?)?/$ index.php?var1=$1&var2=$3&var5=$5 [L]

I’ve tried altering [L] to [L,R=permanent] but this makes all my links and pages not found.

I tried adding just header(“HTTP/1.1 301 Moved Permanently”); in to the top header but it throws a wobbly too.

I did it one step at a time and removed NC, which worked…

RewriteCond %{HTTP_REFERER} !^http://www.site.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.site.com$    [NC]
RewriteEngine on
RewriteRule ^/?([a-zA-Z_]+)(/([a-zA-Z1-9_]+)(/([0-9]+))?)?$ index.php?var1=$1&var2=$3&var5=$5 [L]
RewriteRule ^/?([a-zA-Z_]+)(/([a-zA-Z1-9_]+)(/([0-9]+))?)?/$ index.php?var1=$1&var2=$3&var5=$5 [L]

BUT, when I then add [L,R=301] to it, my site stops working and every link goes to a 404!! Like;

RewriteRule ^/?([a-zA-Z_]+)(/([a-zA-Z1-9_]+)(/([0-9]+))?)?$ index.php?var1=$1&var2=$3&var5=$5 [L,R=301]
RewriteRule ^/?([a-zA-Z_]+)(/([a-zA-Z1-9_]+)(/([0-9]+))?)?/$ index.php?var1=$1&var2=$3&var5=$5 [L,R=301]

What am I doing wrong???

lbts,

If you’re getting 404’s, then there MUST be something wrong with your mod_rewrite code.

RewriteEngine on
[COLOR="Gray"]# -FrontPage-

IndexIgnore .htaccess */.??* *~ *# */HEADER* */README* */_vti*

<Limit GET POST>
order deny,allow
deny from all
allow from all
</Limit>
<Limit PUT DELETE>
order deny,allow
deny from all
</Limit>
AuthName site.com
AuthUserFile /home/sitecom/public_html/_vti_pvt/service.pwd
AuthGroupFile /home/sitecom/public_html/_vti_pvt/service.grp[/COLOR]
RewriteCond %{HTTP_REFERER} !^http://www.site.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.site.com$    [NC]
[COLOR="Red"]RewriteEngine on
# Again? Remove this line[/COLOR]
RewriteRule ^/?([a-zA-Z_]+)(/([a-zA-Z1-9_]+)(/([0-9]+))?)?$ index.php?var1=$1&var2=$3&var5=$5 [L]
# capture one or more letters and _'s in first atom, 
# one or more letters, digits (except 0) and _'s in the third atom
# and one or more digits in the fifth atom
# with only the first atom required
# and the third atom required for the fifth
# with no trailing /
[COLOR="Blue"]# ANDed with the {HTTP_REFERER} RewriteCond statements[/COLOR]
RewriteRule ^/?([a-zA-Z_]+)(/([a-zA-Z1-9_]+)(/([0-9]+))?)?/$ index.php?var1=$1&vae2=$3&var5=$5 [L]
# Ditto the above EXCEPT requiring a trailing /
# (BAD IDEA as it requires absolute links in the
# index.php script or HTML's <base> directive)
# AND is NOT subject to the
# RewriteCond {HTTP_REFERER} statements

The 301 would have told you what the redirection was TO and that should have given you an indication of the problem.

Regards,

DK

lbts,

WHY do you think the No Case flag is useful in a RewriteRule?

Try:

RewriteEngine on
# RewriteRule ^/?([a-zA-Z_]+)/([a-zA-Z1-9_]+)$ index.php?var1=$1&var2=$2 [L]
# That's duplicated by the optional atom in the following RewriteRule
RewriteRule ^/?([a-zA-Z_]+)(/([a-zA-Z1-9_]+)(/([0-9]+))?)?/$ index.php?var1=$1&var2=$3&var5=$5 [L]

Regards,

DK

My full HTAccess looks like;

RewriteEngine on
# -FrontPage-

IndexIgnore .htaccess */.??* *~ *# */HEADER* */README* */_vti*

<Limit GET POST>
order deny,allow
deny from all
allow from all
</Limit>
<Limit PUT DELETE>
order deny,allow
deny from all
</Limit>
AuthName site.com
AuthUserFile /home/sitecom/public_html/_vti_pvt/service.pwd
AuthGroupFile /home/sitecom/public_html/_vti_pvt/service.grp
RewriteCond %{HTTP_REFERER} !^http://www.site.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.site.com$    [NC]
RewriteEngine on
RewriteRule ^/?([a-zA-Z_]+)(/([a-zA-Z1-9_]+)(/([0-9]+))?)?$ index.php?var1=$1&var2=$3&var5=$5 [L]
RewriteRule ^/?([a-zA-Z_]+)(/([a-zA-Z1-9_]+)(/([0-9]+))?)?/$ index.php?var1=$1&vae2=$3&var5=$5 [L]

And there is nothing else in your .htaccess?

Ahh DK, Why does it seem you’re the only person on this whole site who knows anything about ModRewrite :slight_smile:

So, I’ve trimmed it so it looks something like;

RewriteCond %{HTTP_REFERER} !^http://www.somedomain.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.somedomain.com$    [NC]
RewriteEngine on
RewriteRule ^/?([a-zA-Z_]+)(/([a-zA-Z1-9_]+)(/([0-9]+))?)?$ index.php?var1=$1&var2=$3&var5=$5 [L,NC]
RewriteRule ^/?([a-zA-Z_]+)(/([a-zA-Z1-9_]+)(/([0-9]+))?)?/$ index.php?var1=$1&var2=$3&var5=$5 [L,NC]

Will this now stop it being a temporary redirect and make it a permanent?
And is there any way of losing the 2nd line to allow for ‘/’ or the lack of in just 1 ??

I was more worried all my search engine efforts will be thwarted by every page on the site now being 301 (as AWstats seems to keep telling me).

Thanks for the help - as always!

Ehm… 301 is a permanent redirect. It’s also the redirect you want to give your PR from the previous location to the new location.

That being said, your script currently doesn’t externally redirect at all. You should change [L,NC] to [L,NC,R=301] and then remove the NC because you really don’t need in the rules – you already have a-zA-Z in the regex so it’s already case insensitive. So, change [L,NC] to [L,R=301] :slight_smile:

But when I try to change them to be 301 all the pages stop working and go to 404s.

Simple: Unless otherwise directed, mod_rewrite makes TEMPORARY redirections (302) rather than PERMANENT redirections (301). Since I don’t see a 301 anywhere above, they’re all temporary.

Regards,

DK

No idea why it’s still making all my pages 302 instead of 301?