SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast BigBlotch's Avatar
    Join Date
    Mar 2005
    Location
    USA
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    mod_rewrite 404 error

    Hi,

    I'm having some trouble getting mod_rewrite to work on my testing/development subdomain for a new project of mine.

    I have a URL: subdomain.mydomain.com/dir1/dir2/file.php?var=value. All I want is to have it look like subdomain.mydomain.com/dir1/dir2/value.

    I've tried everything I can think of, but to no avail. I'm sure that everything is configured correctly in my httpd.conf file since I have mod_rewrite working correctly for another site on the same server.

    I've never had trouble with rewriting URLs in the past, is there some hidden trick when dealing with a subdomain that I don't know about? I'm about to pull my hair out!

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,081
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Create a .htaccess in de document root (where the index.php is) and put these contents in it:

    Code:
    RewriteEngine On
    RewriteRule /?dir1/dir2/([a-zA-Z0-9]*) /dir1/dir2/file.php?value=$1
    Should work IMHO (as long as value can only take characters from a-z, A-Z and 0-9, otherwise change [a-zA-Z0-9])
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

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

    Yes, you need to put the .htaccess in the subdomain's DocumentRoot (kind of what ScallioXTX was saying).

    If you're using Apache 2.x, you don't need the regex's leading /? and should have an END anchor ($). The redirection should not have the leading / and you do need a Last flag after the redirection.

    More info (and examples) in my signature's tutorial article.

    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

  4. #4
    SitePoint Enthusiast BigBlotch's Avatar
    Join Date
    Mar 2005
    Location
    USA
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the replies guys, but I'm still getting that pesky 404 error.

    I tried your code, Scallio. I also took a look at your tutorials (which are very informative btw), dklynn. I even used your little test procedure in my subdomain document root and it worked fine, indicating mod_rewrite is working.

    I'm running Apache/1.3.41 and as seen by dklynn's test and my phpinfo() file, mod_rewrite is up and running. When I go directly to file.php?var=value it works so I know it has nothing to do with the actual file either. Please tell me, what is wrong with the following code?

    Code:
    RewriteEngine on
    RewriteRule ^/?dir1/dir2/([a-zA-Z0-9]*)$ dir1/dir2/index.php?ident=$1 [L]

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

    Bummer! Okay, Apache 1.x makes a difference as you do need the leading / in your regex (if you're going to use the start anchor, ^).
    Code:
    RewriteEngine on
    RewriteRule ^/?dir1/dir2/([a-zA-Z0-9]*)$ dir1/dir2/index.php?ident=$1 [L]
    Your code is just fine as ^/? is accepted by both Apache 1.x AND Apache 2.x so, if your host upgrades (as they should), you'll be all set.

    dir1/dir2/ are static directories so no problem (so long as your index.php script IS located in dir2).

    The ([a-zA-Z0-9]*) atom is lowercase and uppercase letters and digits, zero or more, and is terminated by the end anchor - PERFECT (so long as your value is ONLY letters and digits - no example provided)!

    The redirection is based on the current location (DocumentRoot) so the dir1/dir2 is fine as is the index.php. The ident key is then assigned the value captured by the atom in the regex and the mod_rewrite block is terminated (as in ; or }) by the Last flag.

    At this point, I have to ask for the test URI that you are using for this redirection (and the target URI).

    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 Enthusiast BigBlotch's Avatar
    Join Date
    Mar 2005
    Location
    USA
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow, I fixed the problem. It turns out that there was a copy of the .htaccess file in my second directory and it was apparently messing everything up. As soon as I deleted it, everything works fine.

    Thank you very much for the help, dklynn. Sorry for trying to rack your brain when it was my fault all along!

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

    No worries. 'Glad you got it sorted out.

    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
  •