Forum Moderators: phranque

Message Too Old, No Replies

mod_rewrite with addon domains

         

andytwiz

10:22 pm on Mar 3, 2005 (gmt 0)

10+ Year Member



Hi

I'm using the following simple mod_rewrite:

Code:

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

This works fine when I access my site via the servers host's domain (not my addon domain) and my subfolder e.g.

[mydomain.hostsdomain.com...] -> index.php?page=page_here

However, when i try to access from my addon domain which points to /mydomain/ folder i get a 500 server error

[mydomain.com...] -> 500 Error

The .htaccess file is in the /mydomain/ folder

Any ideas?

sitz

11:49 pm on Mar 3, 2005 (gmt 0)

10+ Year Member



If it's throwing a 500 error, there /should/ be a corresponding message apache's error log; if so, what is it?

andytwiz

1:01 am on Mar 4, 2005 (gmt 0)

10+ Year Member



hi thanks for your reply.

i checked, the only logs i can find under the cpanel "Error Log" contains old 404 errors (I re-created the 500 error but the logs didnt change - maybe they arnt updated in realtime?..)

andytwiz

1:35 am on Mar 4, 2005 (gmt 0)

10+ Year Member



I got rid of the 500 error!

it was the preceeding / before /index.php

However, now the $_GET[] in php has the page as "index.php" rather than whatever comes after the domain.com/page_goes_here

The code now looks like:

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

any ideas why the page_goes_here isn't being put into the page=$1?

thanks

sitz

4:44 am on Mar 4, 2005 (gmt 0)

10+ Year Member



Sure; when you issue the rewrite, the request has been rewritten, so the URL you're requesting becomes 'index.php', which is what gets shoved into $_GET[]; the information you want is stored in $_SERVER["REDIRECT_QUERY_STRING"] (also stored in a couple of other places; write a small PHP script which just calls phpinfo() to see where). To make your code as flexible as possible, I'd test for the existence of $_SERVER["REDIRECT_QUERY_STRING"] (or whatever you decide to use) first, and if it doesn't exist, fall back to the $_GET string. Note that this may NOT be the appropriate thing to do for your particular case; it's a case-dependant sort of thing (but then, isn't everything?) =)

andytwiz

5:26 am on Mar 4, 2005 (gmt 0)

10+ Year Member



you are indeed right :D

thanks :)

andytwiz

5:29 am on Mar 4, 2005 (gmt 0)

10+ Year Member



in fact its stored in:

REQUEST_URI /ghd/dfg

but REDIRECT_QUERY_STRING page=index.php
and QUERY_STRING page=index.php

is it safe to assume it will always be in the REQUEST_URI and cant be hacked or spoofed or pished e.t.c?

is there another way to do this without a redirect so it can be passed directly into $_GET?

thanks

jdMorgan

5:40 am on Mar 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A problem with the code as posted, and a possible cause of both the incorrect var value and the 500 Server Error is that there is nothing to stop this code from rewriting a URL that it has already rewritten. Specifically, consider blocking the rewrite if the URL-path already contains "index.php". (I should note here that this is needed because mod_rewrite in .htaccess behaves as if it is recursive, due to the way that it interacts with httpd.conf.)

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !index\.php$
RewriteRule ^(.*) index.php?page=$1 [L]

Jim

andytwiz

6:04 am on Mar 4, 2005 (gmt 0)

10+ Year Member



thanks! now $page contains the correct varaible!

:D

andytwiz

6:39 am on Mar 4, 2005 (gmt 0)

10+ Year Member



sorry one final question

how do i match any page that dosnt contain a /? (and if it does contain a / i want to process it with a different rule)

im trying

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

but that doesnt work.

jdMorgan

3:42 pm on Mar 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm sorry, that's not clear. Can you please be more specific about what these URL-paths would look like? (examples are nice.)

Jim