views:

867

answers:

5

Hey folks,

I am betting on an obvious problem here I am not seeing.

Here's the important bits for those of you familiar with Mod-Rewrite

.htaccess file with mod-rewrite rules exists here: http://www.thedomain.com/.htaccess User goes to this URL: http://www.thedomain.com/test/blog Mod-Rewrite rules should actually tell the server to access this URL: http://www.thedomain.com/index.php?page=blog

.htaccess:

Options FollowSymLinks
Options -MultiViews
RewriteEngine on
RewriteRule ^test/([^/.]+)$ /index.php?page=$1 [L]

This combination of code/request does not work. If you're wondering about the code snippet ^test not being ^/test instead, it is because apparently this is a problem on GoDaddy, the code fails with the / after the ^ - this seems like it may be related to my problem, which I'll explain further... If I change the .htaccess code line:

RewriteRule ^test/([^/.]+)$ /index.php?page=$1 [L]

to

RewriteRule ^test([^/.]+)$ /index.php?page=$1 [L]

(just removing the / here: ^test/([^/.]+) )

The code works when the requested URL is changed to accomodate (remove the slash; http://www.thedomain.com/testblog) as the user views the proper index.php?page=blog server response. It seems to me I cannot use any slashes within the darn match side of the RewriteRule. What gives?

Update: If at all relevent, this .htaccess file and the relevant files to the question all exist in a subdirectory off of the GoDaddy server that is hosting this although the domain points to the subdirectory as the root. Not sure if this is relevant.

Update: This server (at the server root) is actually running wordpress with pretty URLs enabled and they work perfectly fine. I assume wordpress uses mod-rewrite to make crazy urls like thedomain.com/2008/11/15/the-article-title.html work...?

Thanks so much.

+1  A: 

Is RewriteBase what you're looking for?

Matijs
I just set the RewriteBase to the subdirectory that all of this exists inside (see the comment I added just a moment ago to the original post) and it seems to make no difference. As soon as I add a / within the match URL in the RewriteRule - it breaks.
Chris Cooper
+1  A: 

there is a nice test utility for windows here

http://www.helicontech.com/download-isapi_rewrite.htm

try changing your code to:

^/test/([^/]+)$ /index.php?page=$1 [L]

or without slashes

^test[^a-z]+([a-z]*)$ /index.php?page=$1 [L]
Josh
Thanks for providing the useful link to the Regex tester, but the code passes. As mentioned, adding a / after the ^ causes the server to return an error; apparently this a common problem on GoDaddy for Mod-Rewrite. I guess maybe this problem is also the same for adding a / anywhere in the match expression.
Chris Cooper
try this regexp - doesn't use slashes test[^a-z]+([a-z]*)$
Josh
Your new ones in the original comment don't work.The one you just posted (test[^a-z]+([a-z]*)$) SHOULD work, but doesn't. Same problem. Don't understand.
Chris Cooper
A: 

If you're wondering about the code snippet ^test not being ^/test instead, it is because apparently this is a problem on GoDaddy, the code fails with the / after the ^ […]

That’s not odd but necessary:

Per-directory Rewrites

When using the rewrite engine in .htaccess files the per-directory prefix (which always is the same for a specific directory) is automatically removed for the pattern matching and automatically added after the substitution has been done.

And that per-directory prefix is for a .htaccess file in the document root (/.htaccess) the URL path root (/). Thus patterns with the ^ must be written without that per-directory prefix /.

On the same way the substitution is handled. After a rule is applied, the per-directory prefix is added to the substituion. So try this rule:

RewriteRule ^test/([^/.]+)$ index.php?page=$1 [L]
Gumbo
Isn't that what I started with, plus a + in there..? -> RewriteRule ^test/([^/.]+)$ /index.php?page=$1 [L]
Chris Cooper
Sorry, additionally it didn't appear to "fix" anything.
Chris Cooper
A: 

OK, first off, I think that the GoDaddy apache server simply has some of the options turned off. I think that if they don't have an AllowOverride FileInfo in their configuration, RewriteRule won't work so well, or at all.

Which means its surprising that the URL http://www.thedomain.com/testblog works at all, and gets re-written. So I guess I'm a little confused.

Here's an idea: Try creating a directory named test, and put the .htaccess file in there! It would look like this:

Options FollowSymLinks
RewriteEngine on
RewriteRule ^([^/]+)$ /index.php?page=$1 [L]

OK, another idea: Use RewriteCond. Maybe you can check the request URI directly, like this:

Options FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/test/([^/]+)
RewriteRule . /index.php?page=%1 [L]

Last idea: maybe your browser sees the URL http://www.thedomain.com/test/blog and thinks it's a directory, and adds a slash? So the URL is sends is http://www.thedomain.com/test/blog/. In that case, the REGEX won't match unless you allow for a trailing slash:

RewriteRule ^test/([^/.]+)/?$ /index.php?page=$1 [L]

Whoops. Sorry for gushing - there's just some many things that can go wrong in an HTTP request that goes through rewriting, and as many ways to try and overcome the problems :-)

scraimer
I didn't try writing another .htaccess and creating a /test/ dir, then sticking it in there... I suspect that will work but of course, it doesn't really help. I won't actually use this for something I could create a dumby directory structure with.I tried using the RewriteCond as suggested with no change to the problem, yet I also could not use the RewriteCond to make the 'testblog' (versus 'test/blog/') request work either, despite the obvious code change to: RewriteCond %{REQUEST_URI} ^test([^/]+)
Chris Cooper
And thanks for gushing! This hasn't been easy. Last comment got character limit stomped out!
Chris Cooper
A: 

I was unable to find a solid method around this problem on GoDaddy; for whatever reason I could not have slashes within the URL that was attempting to be rewritten aside from the base (http://www.somedomain.com/testingthis would work but http://www.somedomain.com/testing/this died).

I ended up instead using the Wordpress .htaccess to send all non-existant file/directory requests back to my index.php. I then used the $_SERVER['REQUEST_URI'] var with pathinfo() to parse the URL and then direct what content to load from the parsing. This works well, is fast, and is probably the same method Wordpress uses.

Thanks for the attemps!

Chris Cooper