Forum Moderators: phranque

Message Too Old, No Replies

mod-rewrite and .htaccess

         

Teschio

1:05 pm on Sep 10, 2004 (gmt 0)

10+ Year Member



Hi to all!
I have some problems with apache mod rewrite, i'd like to create a .htaccess file for my purpose but i don't know what do i have to write in it...
That's what do i like to do:

I have a domain example.com (with cpanel) and two subdomains under it (forum and test)...
I'd like that when I write:

[example.com...] ===> do nothing, just go to the index (as default, it do that also without .htaccess file)
[example.com...] ===> do nothing, just go to the index (as default, it do that also without .htaccess file)
[forum.example.com...] ===> do nothing, just go to the index (as default, it do that also without .htaccess file)
[forum.example.com...] ===> do nothing, just go to the index (as default, it do that also without .htaccess file)
[test.example.com...] ===> do nothing, just go to the index (as default, it do that also without .htaccess file)
[test.example.com...] ===> do nothing, just go to the index (as default, it do that also without .htaccess file)

AND when I write:

[anywordhere.example.com...] ===> redirect to [test.example.com...]
[anywordhere.example.com...] ===> redirect to [test.example.com...]

Note that there isn't any "anywordhere" folder on my root, I don't know in this case which is the word before .example.com, users can write what do they want... then, when redirected to "http://test.example.com/script/index.php?sub=word_choosen" if the word exist (in my database) they get their page if not they get a customized 404 error page...

Can you help me to write the .htaccess file?
Thank you a lot!

jdMorgan

3:40 pm on Sep 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Teschio,

Welcome to WebmasterWorld!

There are links to basic references in our charter [webmasterworld.com]. The best start is to read all of that material, and then work with simple examples until you are comfortable with mod_rewrite. Due to the volume of requests on this forum, we cannot write your code for you, but we'll be happy to help you get your code working.

Jim

Teschio

4:16 pm on Sep 10, 2004 (gmt 0)

10+ Year Member



I have already read that material... i understand not a lot... can you give me any link about tutorials for dummies?

jdMorgan

6:36 pm on Sep 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Did you find the examples in the URL rewriting guide? If those are of no help, then I don't know what to recommend, except maybe this thread [webmasterworld.com] or a search [google.com].

Jim

Teschio

2:42 am on Sep 11, 2004 (gmt 0)

10+ Year Member



I tried this code:

RewriteEngine on
RewriteCond %{HTTP_HOST}!^www\.example\.com
RewriteCond %{HTTP_HOST}!^forum\.example\.com
RewriteCond %{HTTP_HOST}!^www\.forum\.example\.com
RewriteCond %{HTTP_HOST}!^test\.example\.com
RewriteCond %{HTTP_HOST}!^www\.test\.example\.com [NC]
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com$
RewriteRule ^(.+)$ [test.example.com...]

But when I write for example [aaa.example.com...] (aaa does not exist) I am not redirected to [test.example.com...] it returns a 404 error..

Where is the error?

jdMorgan

4:51 am on Sep 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The problem is likely that your RewriteRule pattern "^(.+)$" requires that there be something (one or more characters -- for example, "index.html") in the local filepath part of the URL. So the rule fails for requests for www.example.com/ because the local path part in that request is empty.

Also, since you do not intend to create or use a back-reference to the local URL-path, and no "local OR" is needed, you need not enclose the pattern in parentheses.

You stated that you want to do a redirect (as opposed to an internal rewrite), so you should add the [R=301] flag to the end of the rule. And unless you want to process the modified URL with additional rules after deciding to do the redirect, you should add the [L] flag as well, to terminate mod_rewrite processing for this HTTP request if the rule is invoked.

With those changes, we have:


RewriteRule .* http://test.example.com/script/index.php?sub=%1 (R=301,L]

Your last RewriteCond also has a latent problem. The pattern will fail if the user, the user's browser, or an intervening proxy adds a port number to the request, e.g. sample.example.com:80
So, I recommend removing the $ end-anchor from that RewriteCond. Also, that particular RewriteCond must be the final RewriteCond, because that is the one that your "%1" back-reference will refer to.

Finally, you can make use of the "local OR" to compress your RewriteConds. Rolling the whole thing up, we have:


RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} !^(www\.)?(forum¦test)\.example\.com
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com
RewriteRule .* http://test.example.com/script/index.php?sub=%1 (R=301,L]

Change the broken pipe "¦" character above to a solid pipe before use. Posting on this board modifies them.

Note that using a redirect requires handshaking with the client browser, and will change the URL in the browser's address bar. This may or may not be what you really want. If not, the rule will need to be changed by removing "http://test.example.com" and the "R=301,".

Jim

Teschio

1:57 pm on Sep 11, 2004 (gmt 0)

10+ Year Member



I tried with your code:

RewriteCond %{HTTP_HOST}!^www\.example\.com
RewriteCond %{HTTP_HOST}!^(www\.)?(forum¦test)\.example\.com
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com
RewriteRule .* [test.example.com...] [R=301,L]

But it still doesn't work... If I try to get a subdomain that doesn't exist, the response is a 404 error...

jdMorgan

3:02 pm on Sep 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK, let's back up a bit...

Do you have any other mod_rewrite rules that are working?

If not, try a simple one first, rather than jumping in with this one that involves a script.

If not, it may be that you don't have mod_rewrite enabled. mod_rewrite requires that either the FollowSymLinks or SymLinksIfOwnerMatch option be enabled. So, it may be that you just need to add


Options +FollowSymLinks

ahead of the RewriteEngine directive in your code.

Jim

Teschio

5:29 pm on Sep 11, 2004 (gmt 0)

10+ Year Member



Nothing to do... same 404 error...

Teschio

2:31 pm on Sep 12, 2004 (gmt 0)

10+ Year Member



No more ideas? I searched the web for more examples but no one fit for me.... they don't work!

jdMorgan

4:52 pm on Sep 12, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



From message #8:

Do you have any other mod_rewrite rules that are working?

If not, try a simple one first, rather than jumping in with this one that involves a script.

Create and upload a simple html page called "hello.html" with a "hello world" message on it.
Add this code at the top of your .htaccess file:


Options +FollowSymLinks
RewriteEngine on
RewriteRule ^silly\.html$ /hello.html [L]

Now type "http://www.<yourdomain>.com/silly.html" into the address bar of your browser, and click "Go."
You should see the contents of "hello.html". If not, you either don't have mod_rewrite installed, or there is a configuration error in your httpd.conf file.

You should also check the contents of your server error log file whenever you get an error. The messages in that file are often very helpful in finding problems.

Jim

Teschio

6:47 pm on Sep 12, 2004 (gmt 0)

10+ Year Member



It works, i can see hello.html content

jdMorgan

7:01 pm on Sep 12, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK, now try this, but put a valid value into the "sub=" below:

RewriteRule ^silly.html$ /script/index.php?sub=<something_valid> [L]

If this fails, it's likely that the LoadModule order in your httpd.conf file is incorrect, and that php is loaded after mod_rewrite. In this case, php will be invoked first, and mod_rewrite will never run.

Jim

Teschio

11:23 pm on Sep 12, 2004 (gmt 0)

10+ Year Member



It works

Teschio

3:19 pm on Sep 13, 2004 (gmt 0)

10+ Year Member



Now that we tested that my httpd.conf is ok, is there a method to get the script running?

jdMorgan

3:36 pm on Sep 13, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Now try it with an external redirect. Put a valid value into the "sub=" below:

RewriteRule ^silly.html$ http://test.example.com/script/index.php?sub=<something_valid> [R=301,L]

Teschio

3:45 pm on Sep 13, 2004 (gmt 0)

10+ Year Member



It doesn't work with:
non existent subdomains...

RewriteRule nonexistentsubdomain.example.com --> redirected to [test.example.com...] [R=301,L]

jdMorgan

3:57 pm on Sep 13, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm sorry, I don't have a lot of time to help with individual problems like this. Please try the code as I requested, with a valid subdomain. Also post the resulting line from your server error log if you can get it.

Jim

Teschio

6:44 pm on Sep 13, 2004 (gmt 0)

10+ Year Member



I think I explained my problem not very well:

Now I try to explain better and in few words now.

1) Now You go to [jdmorgan.mysite.com...]
2) I don't know you, and I never made the subdomain "jdmorgan" under "mysite.com"
3) The server response is a 404 error (cannot find ecc...)

I'd like that:

4) The server must not response with a 404 error (cannot find ecc...)
5) The server must redirect you to [mysite.com...]

Now I don't know how the code:


RewriteRule ^silly.html$ http://test.example.com/script/index.php?sub=<something_valid> [R=301,L]

can help me.

Can you help me now, please?
Thank you a lot

jdMorgan

8:56 pm on Sep 13, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Now I don't know how the code:
RewriteRule ^silly.html$ [test.example.com...] [R=301,L]
can help me.

It can help you because I was asking you to test in order to find out why the code above did not work. If you don't want to test and report the results, there is not much I can do to help you with that problem.

1) Now You go to [jdmorgan.mysite.com...]
2) I don't know you, and I never made the subdomain "jdmorgan" under "mysite.com"
3) The server response is a 404 error (cannot find ecc...)

I'd like that:

4) The server must not response with a 404 error (cannot find ecc...)
5) The server must redirect you to [mysite.com...]

This code will rewrite any request to any (sub)domains to your script, except:
mysite.com
www.mysite.com
test.mysite.com
forum.mysite.com
www.test.mysite.com
www.forum.mysite.com
Requests for those (sub)domains will not be rewritten.

For any other non-blank (sub)domains, control will be passed to your script with the requested subdomain and page as parameters. Your script must determine if the subdomain name is valid, and return either the requested content with a 200-OK status code or a 404-Not Found error response.


Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^(www\.)?(forum\.¦test\.)?mysite\.com
RewriteCond %{HTTP_HOST} ^(.+)\.mysite\.com
RewriteRule (.*) /script/index.php?sub=%1&page=$1 [L]

Change the broken pipe "¦" character above to a solid pipe before use. Posting on this board modifies them.

In order to work, this code must be executed. It will only be executed if it is placed in a directory that the server accesses for all requests. If you use a "control panel" to create subdomains on your server, then the code may not work. It depends on how the control panel creates subdomains in the file space of your server.

Also, as I noted, if the server module load list loads the Apache modules in the incorrect order, then the code will not work because it won't be executed.

This is a slightly-different version of the code I posted above. Use it as an example to learn mod_rewrite and then modify it to suit your needs. If it does not work as shown, then you have a problem with the configuration of your server, and should contact your hosting company for help.

Jim

Teschio

11:26 am on Sep 14, 2004 (gmt 0)

10+ Year Member



First thank you for your support...
It still don't work...
I launched a phpinfo() and I noted that the voice:
Virtual Directory Support is turned to 'disabled'.
Could this be the problem?
Now I have to call my server admin... which httpd.conf modification do I have to tell him to do?
Thanks a lot!

Scagnetti

9:13 pm on Sep 14, 2004 (gmt 0)



May seem like a silly idea, but, have you got a wildcard setup in DNS for your server so that any undefined queries return the IP of your webserver?

Teschio

11:47 am on Sep 15, 2004 (gmt 0)

10+ Year Member



I don't know... how do i check it?

Teschio

11:57 am on Oct 8, 2004 (gmt 0)

10+ Year Member



I asked the customer support... they told me on httpd.conf everything is ok...
What's the problem?
I'm getting crazy...
It's several days I try... and try... and try...

jdMorgan

5:09 pm on Oct 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Check your DNS [rscott.org]

Jim