SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,033
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    How to make a home page redirect

    Hi,

    We want people going to:

    www.anooz.com/sarah

    to be redirected to:

    www.anoox.com/aa_redirect.php?name=sarah

    but of course we do not want someone going to:

    www.anooz.com/about_us.php

    to be redirected.


    I have set up this .htaccess but it is not working:


    Options +FollowSymLinks
    RewriteEngine on
    RewriteBase /
    RewriteRule ^/(.*)$ aa_redirect.php?name=$1


    What needs to be corrected in above code?
    ThanX

    Anoox search engine volunteer

    www.anoox.com

  2. #2
    SitePoint Member
    Join Date
    Apr 2013
    Posts
    12
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Use .htaccess for any redirects

  3. #3
    SitePoint Member
    Join Date
    Apr 2013
    Posts
    12
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Sorry I didn't read your comment properly. One not use cPanel inbuilt redirection. This will set the values correctly. If all else fails, delete the access file and rebuild it which will take care of any syntax errors, but then you lose any previous saves

  4. #4
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,033
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    1st, we do not use Cpanel.
    2nd, this issue is something that should be strictly addressed by the .htaccess file. My only question is what I am doing wrong in the above code as a result of which redirect is failing?

    FYI: the redirect works correctly when entered as a sub-dir of root, like:

    Options +FollowSymLinks
    RewriteEngine on
    RewriteBase /
    RewriteRule ^blog/(.*)$ aa_redirect.php?name=$1

    but it is not working correctly for the root dir itself, which again is my Q as to why?

    Anoox search engine volunteer

    www.anoox.com

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

    Have you read the sticky post OR the mod_rewrite tutorial linked in my signature (a better version of the sticky posts)? That would have answered the question for you.

    Code:
    Options +FollowSymLinks
    RewriteEngine on
    RewriteBase /
    RewriteRule ^/(.*)$ aa_redirect.php?name=$1
    That won't work simply because it's loopy code (and Apache 2 refuses/refused to match the leading /).

    [standard rant #1]
    The use of "lazy regex," specifically the EVERYTHING atom, (.*), and its close relatives, is the NUMBER ONE coding error of newbies BECAUSE it is "greedy." Unless you provide an "exit" from your redirection, you will ALWAYS end up in a loop!
    [/standard rant #1]

    Okay, specification:

    Quote Originally Posted by WorldNews View Post
    sarah => aa_redirect.php?name=sarah but NOT about_us.php
    Your specification is insufficient as "sarah" is likely to be a variable. Above, you've constructed the variable with ONLY lowercase letters. This is easily differentiated from about_us.php by both the _ and the dot character. However, mod_rewrite provides a "safety value" in that it allows you to check whether the request was for a file which exists. Taking advantage of these "refinements,"

    Code:
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f # to check on an existing file
    RewriteCond %{REQUEST_FILENAME} !-d # to check on an existing directory
    RewriteRule ^([a-z]+)$ aa_redirect.php?name=$1 [L]
    If you're not sure what version of Apache you're using, replace the ^ (or your ^/) with ^/? as this will work with both Apache 1.x and Apache 2.x.

    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
  •