Jump to content

Mod Rewrite... Ugh!


Recommended Posts

Hi all,

I'm trying to setup a rewrite rule set so that when someone tries to access ANY subdomain, they are sent to a specific directory. Not only that, but the original subdomain they typed remains in the browser address bar as they browse each of the directory files.

 

Meaning:

1. user types in bob.example.com

2. user acually arrives at example.com/members.index.php though bob.example.com/index.php appears in the address bar.

3. user clicks on a link to another page in the same dir and the address bar continues to show the subdmain ex: bob.example.com/guestbook.php

 

Is this possible?

 

I already have wildcard setup so that any subdomain resolves to the root. Was this necessary? Perhaps I should begin with the wildcard resolving to the members dir?

 

Any help is greatly appreciated!

Link to comment
Share on other sites

Unfortunately not, but thanks.

 

Another problem I'm having is that no matter what type of rules I write in the .htaccess file, it seems to have absolutely no affect whatsoever... even when I write something that should surely cause an error. Weird.

 

Taking one step at a time, I have tried to get blah.example.com to redirect to example.com/members (with no luck) like this (.htaccess loaded to the root dir):

 

RewriteEngine on

RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com

RewriteRule (.*) http://www.example.com/members/$1 [R=301,L]

Link to comment
Share on other sites

Here's one I'm trying now, but as always... it just resolves to the home page of the root dir:

 

RewriteEngine On

RewriteRule ^\.htaccess$ - [F]

RewriteCond %{HTTP_HOST} !^www\.example\.com?$

RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com?$

RewriteRule ^$ /members/index.php [L]

 

Really stuck on this one!

Link to comment
Share on other sites

welcome to the forums doomwheels :blink:

 

Is this for an account on our servers? I ask because we don't support URL masking.

 

Basically, for what you are trying to do, you will have to edit the PHP to read the URL to interpret it, as well as redirect to the /members/index.php file.

Link to comment
Share on other sites

Hi there,

Yes, it is a TCH account

 

If you are referring to members/index.php when you say "edit the PHP", I have no problems with what to do once I get the url to resolve to the members dir... but that's the problem.

 

I've tried several rewriterules all with the same result... everything resolves to the home page in the root dir obeying the wildcard DNS yet ignoring the rewriterule of the htaccess file in the root dir.

 

Why is this?

Link to comment
Share on other sites

yes, you would need to edit "members/index.php", so that should be fine.

 

Have you got lots of different sub-domains? The easiest way would be to check for each if only a small number, otherwise I think you need separate off the subdomain. What's your domain name, I'll have a quick look

Link to comment
Share on other sites

Lots. each time a new member registers, they will be assigned a url such as bob.example.com.

when they access this URL, I want them to be sent to example.com/members/index.php

 

I already have scripts in place once they arrive.

 

The second step is that I need bob.example.com to continue to show while they brows the files contined in the members dir.

 

Thanks

Link to comment
Share on other sites

So I guess what I'm trying to figure out first is why code like this is not working in my TCH account:

 

RewriteEngine On

RewriteCond %{HTTP_HOST} !^www\.example\.com

RewriteCond %{HTTP_HOST} ([^.]+)\.example\.com [NC]

RewriteRule ^(.*)$ http://www.example.com/%1 [L,R]

 

I've tried this and each variation posted above with absolutely no affect. When I access my domain with subdomain, it obeys the wildcard DNS but ignores the .htaccess.

 

Any ideas?

 

Any mistakes in this code regarding my application?

Link to comment
Share on other sites

If you rewrite it to RewriteRule ^(.*)$ http://www.example.com/members [L,R] then it will not show your sub domain name "bob.example.com" since you are re-writing that out of it, and forcing it to the top level domain.

 

You will also not have the "bob" left in, to be able to interpret it within the PHP as needing the "bob" pages.

Link to comment
Share on other sites

Finally working!

 

Now that the server is recognizing my htaccess files, I have been able to test some rewrite rules. The code below seems to be working well and sends any URL containing a subdomain to my members dir plus keeps the subdomain as the user clicks through the files in that dir.

 

htaccess in the root dir:

 

RewriteEngine on

RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]

RewriteCond %{HTTP_HOST} ([^.]+)\.example\.com [NC]

RewriteRule ^(.*)$ members/$1 [L]

 

However, this alone causes a 500 error unless there is a second htaccess in the members dir. I get that, but the funny thing is that the members htaccess file only requires one short line to work.

 

htaccess in the members dir:

 

RewriteRule ^(.*)$ [L]

 

Just this and nothing else. Seems additional info is ignored, yet it won't work without this bit. Does this make sense to anyone?

Link to comment
Share on other sites

  • 2 weeks later...

This is a late response, and it seems like you have an answer already, but you could have added a subdomain, such as members. Then members.example.com would redirect to www.example.com/members yet the domain name would stay as members.example.com This could be done from cPanel under, subdomains.

 

In the "members" directory you could have added an .htaccess file, or any other files that you might have needed.

 

Sandro

 

PS. As to your question about:

>RewriteRule ^(.*)$ [L]

 

This looks like a catch-all, pretty much any URL that you enter will match this regular expression. Apache loads these rewrites from top to bottom, so if you put any more conditions under this one it will not run because the condition before was already true. If you need to add another condition you would add it before the condition above.

Edited by spadin
Link to comment
Share on other sites

>RewriteRule ^(.*)$ [L]

 

You're right, it is a catch-all.

 

What confuses me is that everything breaks without it. The htaccess file in the root causes a 500 error unless the catch-all htacces file is placed in the members dir.

 

Any TCH gurus have an idea why?

Link to comment
Share on other sites

Forgive me, I don't know if this is the right forum to post this, but it seems to be.

 

I've recently migrated to a Zen-Cart web configuration. All is well, but I've been reading about the downside because of "dirty URLS'. I'm a bit freaked because it took 5 years to get my internet presence where I want it.

 

So, I've heard of using .htaccess and rewrite engine. As I don't write code (only hack it), I would be grateful for any direction, tutorials, FAQs, or whatever, so I can start implementing some sort of solution and protect my internet presence.

 

Thanks in advance,

Laura

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...