Forum Moderators: phranque

Message Too Old, No Replies

mod_rewrite problem - 301 redirect not working with multiple rules

         

skyhawk133

10:57 pm on Nov 17, 2005 (gmt 0)

10+ Year Member



Hello, thanks in advance for the help.

Let me give the quick rundown. I have a .htaccess file to rewrite my long URL's to 'static' URL's. These were working fine until I added a line to redirect my sub-domains to a new folder structure.

So Here's what I need to accomplish:

http://widgets.example.com/show.php?widget=123 -> http://example.com/widgets/showwidget123.htm

But instead it's doing this:

http://example.com/widgets/show.php?widget=123

Here's my .htaccess file:


RewriteEngine On
RewriteRule showwidget(.*)\.htm$ /show.php?widget=$1
RewriteCond %{HTTP_HOST} ^widgets.example.com
RewriteRule (.*) http://www.example.com/widgets/$1 [R=301,L]

So it's re-writing the domain correctly, but it's not changing the filename.

[edited by: jdMorgan at 11:27 pm (utc) on Nov. 17, 2005]
[edit reason] Example.com [/edit]

jdMorgan

12:00 am on Nov 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The rule you show rewrites a 'short' static URL to a 'long' dynamic URL, rather than the reverse, so that's a bit confusing.

The usual technique used is to:

  • Change all links on all your pages to static (short) search-engine-spider-friendly form
  • Use mod_rewrite to *rewrite* the static URLs, when requested from your server, to the form needed to invoke your script
  • (Optional) Use mod_rewrite to *redirect* dynamic URLs, if directly requested by a client (browser or 'bot) to the short, static form.

    This last step speeds up the elimination of your dynamic URLs from the SERPs, and reduces the chance that someone will link to the dynamic page.

    My best guess at what this would look like on your site is:


    RewriteEngine on
    #
    # Externally redirect direct client requests for dynamic URLs to static URLs
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /show\.php\?widget=([^&\ ]+)\ HTTP/
    RewriteRule ^show\.php$ http://widgets.example.com/showwidget%1.htm [R=301,L]
    #
    # Internally rewrite subdomain to separate subdirectory, while preventing 'infinite' loop
    RewriteCond $1!^widgets/
    RewriteCond %{HTTP_HOST} ^widgets\.example\.com
    RewriteRule (.*) /widgets/$1
    #
    # Internally rewrite static showwidgetNN.htm URL requests to show.php script
    RewriteRule ^widgets/showwidget([^.]+)\.htm$ /widgets/show.php?widget=$1 [L]

    Jim
  •