Forum Moderators: phranque
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!
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
Jim
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?
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]
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]
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
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...
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
Jim
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]
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
RewriteRule ^silly.html$ /script/index.php?sub=<something_valid> [L]
Jim
RewriteRule nonexistentsubdomain.example.com --> redirected to [test.example.com...] [R=301,L]
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 you help me now, please?
Thank you a lot
Now I don't know how the code:
RewriteRule ^silly.html$ [test.example.com...] [R=301,L]
can help me.
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