SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Evangelist
    Join Date
    Jan 2005
    Posts
    425
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    url rewrite question

    With htaccess, I want to achieve 2 things:

    301 redirect anybody who enters http://mydomain.com(/*anything*) to www.mydomain.com (note, redirects to homepage) Non www to http://www.mydomain.com

    301 redirect anybody who enters http://www.mydomain.com(/*anything*) to www.mydomain.com (note, redirects any subpage to homepage

    So basically anything other than a direct call to http://www.mydomain.com will 301 redirect you to homepage (http://www.mydomain.com).

    So, yes, this is just a 1 page site.

    Hope that makes sense.

    Thank you.

  2. #2
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,604
    Mentioned
    19 Post(s)
    Tagged
    2 Thread(s)
    LB,

    Specificity: You want to:

    1. Force www "subdomain" in your links

    and

    2. Redirect EVERYTHING to your DirectoryIndex (which I have to assume is index.php - I also have to assume that you'll want to retain the request as "link" in a query string althrough PHP can look at {THE_REQUEST}).
    Code:
    RewriteEngine on
    
    # #1
    RewriteCond %{HTTP_HOST} !^www\.example\.com$
    RewriteRule .? http://www.example.com{REQUEST_URI} [R=301,L]
    # this retains the original request while changing to www'd domain (lowercase)
    
    # #2 - This is the query string version
    RewriteCond {REQUEST_URI} !index\.php$
    RewriteRule ^(.*)$ index.php?link=$1 [L]
    
    # #2a - This is the NON-query string version
    RewriteRule !index\.php$ index.php [L]
    
    # #2b - Merely use ErrorDocument to redirect 404s to index.php (the handler script)
    ErrorDocument 404 /index.php
    # Note that the redirection MUST be absolute - this is "internal absolute"
    My "insistance" on actually using the DirectoryIndex is (a) hidden and (b) prevents forcing Apache to do additional work (more cycles, more delay) for no useful purpose.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator


Bookmarks

Posting Permissions

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