www.webdeveloper.com
Results 1 to 2 of 2

Thread: .htaccess redirect: php to new html docs

  1. #1
    Join Date
    Feb 2007
    Posts
    43

    .htaccess redirect: php to new html docs

    Hello, I'm wondering how to correctly write a 301 redirect in my .htaccess file for the following scenario:

    www.sitename.com/home_state.php?page=management
    www.sitename.com/home_state.php?page=commercial
    www.sitename.com/home_state.php?page=careers

    www.sitename.com/management.html
    www.sitename.com/commercial.html
    www.sitename.com/careers.html

    Thank you.

    My problem is that the home page was:

    www.sitename.com/home_state.php
    When I did the following redirect:
    Redirect 301 /home_state.php http://sitename.com/index.html

    But what happens is all subpages redirect to the home page, but the php address stays in the browser address bar. How do I correct this?

    *these are fake links, just for illustrative purposes

  2. #2
    Join Date
    Feb 2007
    Posts
    43

    Solution

    The solution was to include the following two lines in my htaccess file:

    Code:
    RewriteEngine On
    RewriteBase /
    For exact file names redirects, such as listed above the following code will redirect all instances of this in the site:


    Code:
    RewriteCond %{QUERY_STRING} page=(.+)
    RewriteRule ^home_state\.php http://somesite.com/%1.html? [R=permanent,L]
    In the case where a redirected file name is not exactly the same, individual instances of this code can be included:

    Code:
    RewriteCond %{QUERY_STRING} page=products1
    RewriteRule ^home_state\.php http://somesite.com/products.html? [R=permanent,L]
    The above code would redirect www.somesite.com/home_state.php?page=products1 to www.somesite.com/products.html

    The code was explained to me as follows:
    ^ anchors the rule to the beginning, so it can't ignore anything
    .+ means at least one character, any value, and as many as you can match. Basically it'll grab any text at all.
    The parenthesis matches it for use later in the rewrite rule
    R=permanent is longhand for R=301
    L means last rule, stop rewriting and don't get into a 500 redirection loop.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles