View Full Version : mod_rewrite

11-04-2010, 05:37 PM
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:

# 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:

# 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.

11-17-2010, 01:28 PM
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:

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:

^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:


Again it's not been tested but I think it should work.

11-18-2010, 06:48 PM
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.

11-18-2010, 07:55 PM
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:

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

^([^/\.]+)/?$ 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']

11-18-2010, 08:51 PM
Not sure why, but now the page is giving me a 500 server error.

11-18-2010, 08:53 PM
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.

11-18-2010, 09:04 PM
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:

# Use PHP5 Single php.ini as default
AddHandler application/x-httpd-php5s .php

RewriteRule ^([^/\.]+)/?$ index.php?wizard=$1 [NC,L]

11-18-2010, 10:37 PM
Ok, using the following code:

RewriteRule ^([^/\.]+)/?$ test.php?wizard=$1

and the 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">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>



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



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:

RewriteRule ^harry-potter/$ index.php?wizard=scott-brown

11-18-2010, 11:24 PM
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:

# 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.

11-18-2010, 11:36 PM
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.