SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to re-direct URL ?

    Hi guys, I would like a capability so that when someone visits these URLs,

    http://www.domain.com/somedir/xyz.php
    http://www.domain.com/somedir/info.php

    they will be respectively re-directed to:

    http://www.domain.com/somedir/main.php?p=xyz
    http://www.domain.com/somedir/main.php?p=info

    Any idea on how to do it in Apache or PHP ? Thanks.

  2. #2
    SitePoint Addict sdleihssirhc's Avatar
    Join Date
    Feb 2009
    Posts
    387
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    What you're looking for is Apache's mod_rewrite. Here's a great beginner's article on URL rewriting from a pretty cool site, but here's the basic gist:

    You use regular expressions to parse the URL that the user requested, and tell the server which page it should actually send.

    If you have mod_rewrite enabled on your server, you can just put the following in an .htaccess file located in your somedir folder:

    Code:
    RewriteEngine on
    RewriteRule ^/?([a-z]+)\.php$ main.php?p=$1
    I'm the web overlord for Graphic Business Systems

  3. #3
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks sdleihssirhc for your prompt reply. I'm a bit concerned with your regular expression though. I want only the files located under the "somedir" directory to be re-directed and not the files located outside of this directory. For example, when someone visits this link:

    http://www.domain.com/somedir/info.php

    it will be re-directed to: http://www.domain.com/somedir/main.php?p=info

    But when he visits this site:

    http://www.domain.com/somepage.php

    it should not be re-directed.

    Does your regex cover this case as well ?

    Thanks a lot again.

  4. #4
    SitePoint Addict sdleihssirhc's Avatar
    Join Date
    Feb 2009
    Posts
    387
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Just make sure that you put your .htaccess file inside the somedir folder and you should be okay.
    I'm the web overlord for Graphic Business Systems

  5. #5
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,671
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Ah, sdleihssirhc, your solution is "loopy." You always must check the regex to see whether it'll match the redirection, too, and, if it does, provide an exit strategy (with a better regex OR a RewriteCond).

    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

  6. #6
    SitePoint Addict sdleihssirhc's Avatar
    Join Date
    Feb 2009
    Posts
    387
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dklynn View Post
    Ah, sdleihssirhc, your solution is "loopy." You always must check the regex to see whether it'll match the redirection, too, and, if it does, provide an exit strategy (with a better regex OR a RewriteCond).
    Can you explain what about the rule puts it at risk for looping? I tried accessing main.php, thinking that maybe the regex doesn't take parameters into account (so if you accessed main.php, it would try to load main.php?p=main, which would trigger with main.php again), but everything seemed to work fine.
    I'm the web overlord for Graphic Business Systems

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

    Sure. Let me lead you through your code:
    Code:
    RewriteEngine on
    RewriteRule ^/?([a-z]+)\.php$ main.php?p=$1
    RewriteEngine on is perfect.

    The RewriteRule's regex will match (Apache 1 or Apache 2 - good!) a lowercase letter filename with a php file extension. It will then redirect to main.php with a query string.

    The problem is that main.php will be matched by {filename}\.php and redirected to main.php with a query string of p=main ... until Apache 2 recognizes the loop or Apache 1 gets a restart.

    Note 1: Because .htaccess is a per-directory resource, Apache will be looking for (and matching) filename.php then main.php then main.php then ...

    Note 2: This is very similar to the effect that (.*) has when redirecting to the same directory with the only difference being that you are aware enough to use good regex in your RewriteRule (although, these days, the /? isn't necessary because there are very few Apache 1.x servers remaining).

    You've done a good job (with the exception of the loop), though. My solution would be very similar:
    Code:
    RewriteEngine on
    # Prevent the loop by requiring that there be no query string, i.e.
    # RewriteCond %{QUERY_STRING} ^$
    # OR that the file requested is not main.php (very slight preference)
    RewriteCond %{REQUEST_URI} !main\.php$
    # I prefer my .htaccess to be in the DocumentRoot so I've got to include the somedir
    RewriteRule ^somedir/([a-z]+)\.php$ somedir/main.php?p+$1 [L]
    I hope that answers your question (and provides a useable solution for tmyonline).

    For tmyonline, may I offer the tutorial Article located in my signature -- it's helped members here for years.

    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

  8. #8
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks guys, I feel good to have two experts, dklynn and sdleihssirhc, helping me.

    Quote Originally Posted by dklynn wrote
    For tmyonline, may I offer the tutorial Article located in my signature -- it's helped members here for years.
    Sure dklynn, where can I find it ? Thanks.

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

    Try clicking on the word "Article" in my signature.

    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
  •