Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10

Thread: mod_rewrite

  1. #1
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    mod_rewrite

    I have a subdomain attached to my domain that I am trying to use mod_rewrite on. The domain is [mydomain].com and the subdomain is wizard.[mydomain].com which is located at [mydomain].com/wizard.

    I have an .htaccess file at the root domain. It's contents are as follows:

    Code:
    # Use PHP5 Single php.ini as default
    AddHandler application/x-httpd-php5s .php
    I also have an .htaccess file within [mydomain].com/wizard and it's contents are:

    Code:
    # Use PHP5 Single php.ini as default
    AddHandler application/x-httpd-php5s .php
    
    RewriteRule ^/([^/\.]+)/?$ /index.php?wizard=$1 [L]
    I don't know much about mod_rewrite yet, or about regex, but I can't see what I'm doing wrong in my second .htaccess file located under the wizard folder.

    I'm pretty sure mod_rewrite is setup correctly. I'm using bluehost and if I put garbage into the htaccess file it crashes the server.

    Problem: My problem is that the ReWrite rule doesn't seem to be working and I keep getting a 404 error when I type either wizard.[mydomain].com/wizard-x or [mydomain].com/wizard/wizard-x. I want wizard.[mydomain].com/wizard-x to rewrite to wizard.[mydomain].com/index.php?wizard=wizard-x. I want [mydomain].com/wizard/wizard-x to rewrite to [mydomain].com/wizard/index.php?wizard=wizard-x

    Any help would be much appreciated. Thank you.
    Last edited by GoneIn20Seconds; 11-05-2010 at 04:28 PM. Reason: Clarification

  • #2
    New Coder
    Join Date
    Jan 2010
    Location
    In a dump
    Posts
    64
    Thanks
    9
    Thanked 2 Times in 2 Posts
    Basically the reason you are getting a 404 is that you are tying the url wrong.

    The code is fine sort of. When you have wrote:
    Code:
    RewriteRule ^/([^/\.]+)/?$ /index.php?wizard=$1 [L]
    ^/([^/\.]+)/?$ This bit of the code is looking for a folder name to pass to the code. You should really write it:

    Code:
    ^wizard/([^/\.]+)/?$ index.php?wizard=$1 [L]
    I think, thats not 100% tested. Then the test will look for any url starting with /wizard then the variable you want passing to the $_GET array.

    Like www.domain.com/wizard/magic/ note that it will only match the url if there is a slash at the end of the url because it has been put into the query. If you wanted it to match it without the end slash you would need to remove the end slash:

    Code:
    ^wizard/([^/\.]+)?$
    Again it's not been tested but I think it should work.

  • Users who have thanked InterbredMonkey for this post:

    GoneIn20Seconds (11-18-2010)

  • #3
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    InterbredMonkey, thanks for helping me with the regex. It still isn't doing what I expect it to unfortunately and maybe I'm completely missing how to use .htaccess redirects.

    Using the regex you have listed, should I be able to type the URL "http://www.[domain].com/wizard/helloWorld" and then the page at "http://www.[domain].com/wizard/index.php will get the GET variable "wizard=helloWorld"?

    If not I'm completely misunderstanding mod_rewrite.

  • #4
    New Coder
    Join Date
    Jan 2010
    Location
    In a dump
    Posts
    64
    Thanks
    9
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by GoneIn20Seconds View Post
    Using the regex you have listed, should I be able to type the URL "http://www.[domain].com/wizard/helloWorld" and then the page at "http://www.[domain].com/wizard/index.php will get the GET variable "wizard=helloWorld"?
    This is true. However like I explained before I think the code will only get the variable hello world when it is followed by the / for example:
    www.domain.com/wizard/helloWorld/

    Sorry just thought if the htaccess file is in the wizard folder you should just be able to write:

    Code:
    ^([^/\.]+)/?$ index.php?wizard=$1 [NC L]
    ^([^/\.]+)/?$ - Explained a bit to the extent I understand.

    ^ - Starts the match search
    ([^/\.]+) - Tells to match anything that is not a backslash or a forwardslash and the plus symbol means add the next bit on.
    /? - means append a forward slash to the search term, so it still looks for the forward slash.
    $ - means stop the search.

    $1 - This captures the search term variable in the ^([^/\.]+)/?$ and then assigns it to the $_GET['wizard']

  • #5
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Not sure why, but now the page is giving me a 500 server error.

  • #6
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Ok, never mind that last. I think it is because there wasn't a comma between the two flags. [NC,L]

    Unfortunately I'm still getting a 404 error.

  • #7
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I don't know if this would help, but I'm getting the following error in my server logs:

    File does not exist: /home4/[fake_path]/public_html/wizard/flash-caption-wizard

    My .htaccess file is located within the wizard folder and looks like this:
    Code:
    # Use PHP5 Single php.ini as default
    AddHandler application/x-httpd-php5s .php
    
    RewriteRule ^([^/\.]+)/?$ index.php?wizard=$1 [NC,L]

  • #8
    New Coder
    Join Date
    Jan 2010
    Location
    In a dump
    Posts
    64
    Thanks
    9
    Thanked 2 Times in 2 Posts
    Ok, using the following code:

    Code:
    RewriteRule ^([^/\.]+)/?$ test.php?wizard=$1
    and the PHP code:

    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>

    <body>

    <?php

        
    echo("<p>The Variable wizard is ".$_GET['wizard']."</p>");

    ?>

    </body>
    </html>
    I was able to make it work. Example: www.encodedws.co.uk/preview/harry-potter

    Try copying the code and seeing if this works if not have you got other mod_rewrite's that are working?

    If not try something simple like:

    Code:
    RewriteRule ^harry-potter/$ index.php?wizard=scott-brown
    Last edited by InterbredMonkey; 11-18-2010 at 09:42 PM. Reason: Additional.

  • #9
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    The problem has been solved. Whew, that took forever.

    InterbredMonkey, I couldn't have done it without you. You helped with the regex a bit, you helped me to understand .htaccess redirects a bit more, and you confirmed a lot of things that I already believed I understood in .htaccess.

    I had even tried what you last suggested once in the past, and like now it didn't work again. Somehow though I stumbled on a line on the web which I thought I'd try and it fixed it. Without your help I would have still thought that fault was with my rewrite rule, but instead I just needed to add: RewriteEngine On. So, for anyone useing BlueHost to host their site, make sure you add that line.

    Here is my final .htaccess file:
    Code:
    # Use PHP5 Single php.ini as default
    AddHandler application/x-httpd-php5s .php
    
    RewriteEngine on
    RewriteRule ^([^/\.]+)/?$ index.php?wizard=$1 [NC,L]
    Thanks again. Could not have done it without you.

  • #10
    New Coder
    Join Date
    Jan 2010
    Location
    In a dump
    Posts
    64
    Thanks
    9
    Thanked 2 Times in 2 Posts
    Hahahahaha. Yeah that is pretty essential in the htaccess file no matter who your provider is. I cannot believe I missed that. Sorry could of resolved it hours ago. Oh well it just goes to show you should not over look the small things. Glad it's working though.
    The answer my friend is blowing in the wind...


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •