SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Redirect everything

    Hi,

    I need some help creating a RewriteRule. What I want is to be able to do the following:

    http://www.example.com/item1/item2.html

    However, item1 can be anything. I don't want to have to create a rule for each item1 that I may have, I just want everything to be redirected to file.php?item1=something&item2=something

    Edit: I also need it to work if only item1 is specified

    Thanks for any help

    Sean
    Last edited by seanf; Mar 6, 2004 at 11:44.
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  2. #2
    FreeBSD The Power to Serve silver trophy pippo's Avatar
    Join Date
    Jul 2001
    Location
    Italy
    Posts
    4,514
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Sean,

    /.htaccess
    Code:
    RewriteEngine On
    # /everything_except_dot_except_slash.html
    # ==>>
    # /file.php?everything_except_dot_except_slash=something
    RewriteRule ^([^/.]+)./html$ /file.php?$1=something [NC,L]
    
    # /everything_except_dot_except_slash/everything_except_dot_except_slash.html
    # ==>>
    # /file.php?everything_except_dot_except_slash=something&everything_except_dot_except_slash=something
    RewriteRule ^([^/.]+)/([^/.]+)\./html$ /file.php?$1=something&$2=something [NC,L]
    not sure what something could be.
    Also it could be possible that writing:

    /pippo/pippo.html
    it would make a redirection as:
    /file.php?pippo=AA&pippo=BB



    Andrea
    Mr Andrea
    Former Hosting Team Advisor
    Former Advisor of '03

  3. #3
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your help Andrea. Unfortunately I can't get your rules to work, all Apache gives me is 404s. I'm using them like this:

    Code:
    RewriteEngine On
    
    RewriteRule ^([^/.]+)./html$ /others.php?item1=$1 [NC,L]
    RewriteRule ^([^/.]+)/([^/.]+)\./html$ /others.php?item1=$1&item2=$2 [NC,L]
    http://localhost/sean.html = 404
    http://localhost/name/sean.html = 404

    Quote Originally Posted by pippo
    not sure what something could be
    Sorry, something was meant to be the value of item1 and item2

    Any ideas?

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  4. #4
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    And just after I post I spot the error! ./html should be \.html and \./html should be \.html. Is that right?

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  5. #5
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry for so many questions, but there's one more thing I need. I only want the redirection to happen if there isn't a file by the name being called. For example, if I went to www.example.com/real_file.html and I have a file called real_file.html then I don't want to redirect to my PHP script. Can this be done with the -f flag?

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  6. #6
    FreeBSD The Power to Serve silver trophy pippo's Avatar
    Join Date
    Jul 2001
    Location
    Italy
    Posts
    4,514
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok,
    first of all apologizes I typed the rules just when I woke up (last night I was up to watch Ferrari...)...

    You are right...

    Code:
    RewriteEngine On
    
    RewriteRule ^([^/.]+)\.html$ /others.php?item1=$1 [NC,L]
    RewriteRule ^([^/.]+)/([^/.]+)\.html$ /others.php?item1=$1&item2=$2 [NC,L]
    that's fine.

    > Can this be done with the -f flag?
    Yes,
    even if you will add a little overhead to the server, but there are not other ways to do it though with mod_rewrite.

    Story Juice:

    /.htaccess
    Code:
    RewriteEngine On
    
    # if the URI ends with .html
    RewriteCond %{REQUEST_URI} ^/(.+)\.html$ [NC]
    # if the file exists
    RewriteCond %{REQUEST_FILENAME} -f
    # call that file and exit from mod_rewrite
    RewriteRule ^.*$ - [L]
    
    RewriteRule ^([^/.]+)\.html$ /others.php?item1=$1 [NC,L]
    
    RewriteRule ^([^/.]+)/([^/.]+)\.html$ /others.php?item1=$1&item2=$2 [NC,L]
    that should be okay then.


    Andrea
    Mr Andrea
    Former Hosting Team Advisor
    Former Advisor of '03

  7. #7
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There's no need to apologise. Thank you so much Andrea

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature


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
  •