Forum Moderators: phranque

Message Too Old, No Replies

Simple Mod Rewrite 301

         

CWebguy

5:47 pm on Mar 6, 2009 (gmt 0)

10+ Year Member



Hey there, thanks for the help.

I am trying to do a simple 301 from

http://www.example.com/?cat=1
to
http://www.example.com/1

so far I have


RewriteEngine On
RewriteBase /
RewriteRule ^\?cat=([0-9]+)$ /$1 [R]

But can't get it to work. Any help is greatly appreciated (I'm learning).

Thanks for the help!

jdMorgan

6:46 pm on Mar 6, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I hope you reviewed the documentation [httpd.apache.org] first. You cannot "guess" this stuff, and may damage your site's operation and rankings if you try...

RewriteEngine on
#
RewriteCond %{QUERY_STRING} cat=([0-9]+)
RewriteRule ^$ http://www.example.com/%1 [R=301,L]

Jim

CWebguy

8:24 pm on Mar 6, 2009 (gmt 0)

10+ Year Member



thanks Jd, I am going through a couple tutorials online, and trying to learn. This is all on a test server, no worries ;) Thanks a bunch!

Lance

g1smd

9:36 pm on Mar 6, 2009 (gmt 0)

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



*** test server ***

Good Move! ::Thumbs Up!::

Make sure it is behind .htpasswd so that search engines never find it.

CWebguy

9:34 pm on Mar 7, 2009 (gmt 0)

10+ Year Member



If the search engines want to index my localhost, I will happily invite them to. I have ads I can put up ;)

CWebguy

3:52 pm on Mar 10, 2009 (gmt 0)

10+ Year Member



The only problem with this is I'm now getting the query string tacked on to the end!

Example:
example.com/12345?cat=12345

also, if I try example.com/index.php?cat=12345 as the URL it doesn't redirect either. Strange.

What's going one here!?
Thanks.

[edited by: CWebguy at 4:00 pm (utc) on Mar. 10, 2009]

CWebguy

4:25 pm on Mar 10, 2009 (gmt 0)

10+ Year Member



Ok, I think I may have solved it.

A question mark at the end of the rewrite URL seems to clear the query. (I'm hoping)

RewriteRule ^$ http://www.example.com/%1? [R=301,L]

Still doesn't work for index.php?ca=12345, wonder if index.php is considered part of the query?

I also seem to have to put the .htaccess file in each sub folder or it won't even work correctly, wonder if this is a server config error. Thanks.

CWebguy

[edited by: CWebguy at 4:28 pm (utc) on Mar. 10, 2009]

g1smd

4:56 pm on Mar 10, 2009 (gmt 0)

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



^$ matches / and does not match /index.php - that's why the rule does not run.

Fix it by using the index.php-stripping 301 redirect that has been posted in 4 or 5 threads in recent days. That is, you do NOT invoke the rewrite for multiple alternative URLs, you redirect all the alternative URLs to the canonical form of your choosing (presumably with www and without index.php included). This index redirect goes before your general non-www to www redirect.

Note also, that all of the various redirects must be listed before you list any rewrites.

[edited by: g1smd at 5:00 pm (utc) on Mar. 10, 2009]

CWebguy

5:00 pm on Mar 10, 2009 (gmt 0)

10+ Year Member



ok, learning...slowly :) Reminds me of how I learned CSS, eventually. Thanks.

[edited by: CWebguy at 5:06 pm (utc) on Mar. 10, 2009]

CWebguy

5:13 pm on Mar 10, 2009 (gmt 0)

10+ Year Member



Also, about the putting the htaccess in a seperate directory, if I did this


RewriteRule ^subdir/$ http://www.example.com/%1? [R=301,L]

then it worked. I won't bother you anymore :)'

Thanks,
CWebguy

Next stop: Learning the flags!