+ Reply to Thread
Results 1 to 4 of 4

Thread: mod_rewrite helo

  1. #1
    jason32 is offline x10Hosting Member jason32 is an unknown quantity at this point
    Join Date
    Jun 2009
    Posts
    5

    Cool mod_rewrite helo

    I need help!

    I want to redirect http://mysite.com/whatever to http://mysite.com/script.php?p=whatever but ONLY if http://mysite.com/whatever doesn't exist. I'd also (if possible) like to make sure that no trailing "/" is included, in other words http://mysite.com/frrp/ should redirect to http://mysite.com/script.php?p=frrp

    I know that mod_rewrite can do (at least most of) this stuff, but that's as far as my knowledge on the subject goes. I have no idea how to write the rule I need to do this.

    Secondly, I'd like to remove the www. prefix from addresses if a visitor includes it, and redirect http://www.mysite.com to http://mysite.com, regardless of the rest of the path. This is less important, but since I'm asking about mod_rewrite this seemed like a good time to ask!

    Thanks in advance for your help!

  2. #2
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: mod_rewrite helo

    Any solution will rely heavily on RewriteCond. You check %{HTTP_HOST} for a leading "www.", and you can use the "-d" and "-f" to test for directories and files, respectively.

    Try this:
    Code:
    RewriteEngine on
    
    # external redirect; we want the visitor to see that "www." is removed
    RewriteCond %{HTTP_HOST} ^www.(.*) [NC]
    RewriteRule (.*) http://%1/$1 [R=301,L]
    
    # internal redirect. Don't want visitor to see "/script.php?"
    RewriteCond %{DOCUMENT_ROOT}/$1 !-d
    RewriteCond %{DOCUMENT_ROOT}/$1 !-f
    RewriteCond %{DOCUMENT_ROOT}/$1.html !-f
    RewriteCond %{DOCUMENT_ROOT}/$1.php !-f
    RewriteRule ^/?([^/]+)/? /script.php?p=$1 [L,QSA]
    If you want to capture a longer path, you'll have to change the pattern in the last line. If you want to skip files with other extensions, add RewriteConds similar to the last few.

    Note that you should keep the two task (removing "www.", adding "script.php?p=") separate, as you want visitors to see the first but not the second.
    Last edited by misson; 07-10-2009 at 01:19 AM.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  3. #3
    jason32 is offline x10Hosting Member jason32 is an unknown quantity at this point
    Join Date
    Jun 2009
    Posts
    5

    Re: mod_rewrite helo

    Thanks so much mission, this is EXACTLY what I wanted, and you've really helped me out. In fact it's better than what I had in mind - I didn't even know it was possible to hide the result of the redirect from visitors like this.



    Some day I'll learn regular expressions, but for right now they're a bit of a mystery to me.

  4. #4
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: mod_rewrite helo

    Quote Originally Posted by jason32 View Post
    Some day I'll learn regular expressions, but for right now they're a bit of a mystery to me.
    There's no time like the present:
    You should find that learning about finite state automata (aka finite state machines) will be helpful, as FSAs and REs are equivalent.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

+ Reply to Thread

Similar Threads

  1. helo
    By sickcyclecarousel in forum Introductions
    Replies: 4
    Last Post: 01-23-2009, 01:23 PM
  2. helo
    By yousefghethan in forum Free Hosting
    Replies: 1
    Last Post: 10-23-2008, 10:56 AM
  3. helo
    By yousefghethan in forum Free Hosting
    Replies: 1
    Last Post: 10-21-2008, 02:15 PM
  4. Helo All, Fishbowl is here! ^_^
    By Fishbowl444 in forum Introductions
    Replies: 6
    Last Post: 08-02-2008, 07:53 AM
  5. Helo
    By charlesbw in forum Introductions
    Replies: 1
    Last Post: 07-27-2006, 02:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers