Forum Moderators: phranque

Message Too Old, No Replies

Rewrite rule works, 301 redirect doesn't

         

LionMedia

3:21 pm on Sep 9, 2010 (gmt 0)

10+ Year Member



Hi, I have the following rule in .htaccess. The rewrite works fine but it doesn't seem to produce a 301 redirect. What could I be missing?

Rewriterule ^([^-/]+(-[^-/]+)*)-county/([^-/]+(-[^-/]+)*)-md/homes-for-rent-page([0-9]+)\.html$ index.php?action=searchresults&PageID=searchresultssef&County=$1&city=$3&ForSale=N&cur_page=$5 [R=301,L]

Thanks!

jdMorgan

5:32 pm on Sep 9, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Are you sure you want a redirect? Using a 301 redirect will 'inform' the client (such as a browser or search engine) that the URL has been permanently changed and that it should discard the old URL and use the new one instead. In this case, that means that your previous work to create and use 'search engine-friendly' URLs will be destroyed, and visitors and search engines will see and use the ugly script-filepath-plus-query-string URLs from now on.

99.99% of all posters here want to go the other way -- from ugly query-string-based URLs to static-looking "search-friendly" URLs.

To your direct question, though: Do you have other working rules in this file? If not, then you may be missing the required 'setup' directives needed to enable mod_rewrite. Or maybe you've got interference from mod_negotiation, mod_spelling, or AcceptPathInfo.

You will need some variant of both of these directives at the top of your mod_rewrite code, or only the second one. The only way to find out is to test:
Options +FollowSymLinks -Indexes -MultiViews
RewriteEngine on

On Apache 2.x, you may also need:
 AcceptPathInfo off

The Options settings I show in addition to +FollowSymLinks are typical, but you should look them up in the Apache "core" documentation and make sure each is appropriate for your server and site.

Minor problem, but likely non-fatal in this case: You should specify the protocol and hostname for external redirects, and you should not link to "index.php" -- just link to "/" plus the query, and let mod_dir handle the index script filename:
Rewriterule ^([^-/]+(-[^-/]+)*)-county/([^-/]+(-[^-/]+)*)-md/homes-for-rent-page([0-9]+)\.html$ http://www.example.com/?action=searchresults&PageID=searchresultssef&County=$1&city=$3&ForSale=N&cur_page=$5 [R=301,L] 

Jim

LionMedia

6:03 pm on Sep 9, 2010 (gmt 0)

10+ Year Member



Oh I see! I was doing this backwards. I think I understand now.

I do have other rules and I have one directive which has been working: RewriteEngine on

So I tried removing the index.php and added the [mydomain.com...] as you suggested but then the rewrites stop working.

jdMorgan

6:52 pm on Sep 9, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Right, so what is it that you are trying to accomplish here?

Jim

LionMedia

7:02 pm on Sep 9, 2010 (gmt 0)

10+ Year Member



Well I was trying to do the rewrite then permanently redirect any ugly urls that may have been indexed to the new friendly url.