Forum Moderators: phranque

Message Too Old, No Replies

Unique rewrite/redirect questions related to subdomain and more

         

sfnet

6:31 pm on Dec 7, 2007 (gmt 0)

10+ Year Member



I searched the net high and low without success so I am posting here.

I am starting a new forum and I created a subdomain and dropped my forums into it.

I would like to make sure that

Subdomain.mysite.com is the URL which is always displayed when the folder with the files loads. I specifically don't want this url to show EVER even if someone types it in directly:

www.mysite.com/subdomain_folder

that folder contains my forums but my hope is to obtain some rewrite/redirect commands in the htaccess file to ensure only URLs containing subdomain.mysite.com appear in SERPs.

Can anybody help?

I would also like to ensure that either the www version or the non-www version of the site gets indexed while not conflicting with the above commands.

The last and final thing I hope to accomplish is to make the index.php file from vbulletin to never appear in the URLs.

Its a tall order but I know we have some pros here!

sfnet

6:50 pm on Dec 7, 2007 (gmt 0)

10+ Year Member



I thought this might work from what I could piece together but it hasn't:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301]

Redirect /index.php http://subdomain.example.com/

RewriteCond %{THE_REQUEST} \/index.php\ HTTP
RewriteRule ^(.*)$ index.php$/$1 [R=301

[edited by: jdMorgan at 10:17 pm (utc) on Dec. 10, 2007]
[edit reason] example.com [/edit]

sfnet

7:34 pm on Dec 7, 2007 (gmt 0)

10+ Year Member



After reading some more about htacces, I can clarify what I am attempting to do:

Rewrite the following URLs if typed into a browser OR if it is a link to my site somewhere on the Internet:

http://example.com/sub/
http://www.example.com/sub/
http://www.example.com/sub/index.php

to:

http://sub.example.com/

If someone types in http://sub.example.com/index.php, I would like it rewritten without the index.php:

http://sub.example.com/

If any one of the following domains have something after it, like:

http://example.com/sub/exterior-interior/how-to-make-a-blog.html
http://www.example.com/sub/exterior-interior/how-to-make-a-blog.html

Then the command would rewrite the URLs like so:

http://sub.example.com/exterior-interior/how-to-make-a-blog.html

[edited by: jdMorgan at 10:19 pm (utc) on Dec. 10, 2007]
[edit reason] example.com [/edit]

jdMorgan

12:52 am on Dec 8, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Rather than requesting that the members here write your code for you --something we don't do-- and then adding new requirements, I would suggest picking out only one or two primary requirements, and asking very specific questions about the code you have written and tested in an attempt to address them.

It's fairly difficult to address one or two problems, and even harder if the list keeps getting longer.

The subject of redirecting /index.blah to / has been discussed quite frequently here over the past year, and a search [webmasterworld.com] on WebmasterWorld threads would likely get you on-track toward addressing that problem.

Another basic fact is that what shows in the browser address bar and appears in search results is controlled by the links that appear on your pages. No amount of rewrite or redirect code can directly change that. So the first step is to make sure the links on your pages are correct, and then a bit of code can be used to accomplish any necessary "clean-up."

For more information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].

Jim

[edited by: encyclo at 1:30 am (utc) on Dec. 8, 2007]
[edit reason] fixed bbcode error [/edit]

sfnet

1:05 am on Dec 8, 2007 (gmt 0)

10+ Year Member



If you think I did not read the charter, attempt several variations myself, and read several resources, then you are mistaken.

Unfortunately, htaccess is so nuanced that even some people who are experts can get it wrong.

While I appreciate your direction to resources, none have been helpful with the specific set of issues I have faced.

BTW, my third post was merely a clarification of my original post. I had read EVEN MORE after posting the first time and realized I could sum it more succinctly. Since this forum prohibits editing a post, I had to repost.

I apologize for wasting your reading time.

EDIT: I NOW see that there is a way to edit by clicking a small little icon below my username.

sfnet

5:24 am on Dec 10, 2007 (gmt 0)

10+ Year Member



Well, I figured out how to get the www. forced so Google will not index any potential non-www sites:

RewriteEngine on 
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule .? http://www.example.com%{REQUEST_URI} [R=302,L]

Now I need to figure out how to rewrite a link to the subfolder URL as a subdomain URL and rewrite URLs without the index.php. Getting this to mesh together seems one of the biggest challenges.

[edited by: jdMorgan at 10:08 pm (utc) on Dec. 10, 2007]
[edit reason] example.com [/edit]

Marcia

6:24 am on Dec 10, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



how to get the www. forced so Google will not index any potential non-www sites

Actually, you really *don't* want to do that, because forum.example.com is a non-www "host" on the domain and you do want that sub-domain indexed. You might also eventually want to add gallery.example.com and blog.example.com and so forth.

All you really need to do is write a directive so that requests for example.com without the www (example.com/)will return the www. root with a 301.

And also, a directive so that a request for the site with a filename after (index.php, index.htm,index.html) will likewise return www.example.com/ with the forward slash and not the filename. That part is right, but the first thing is getting the if/then logic of the process down, and making use of regular expressions to write the directives. A perfect thread for this situation is this one in the library:

Regular Expressions [webmasterworld.com]

That's how instructions are defined, but the basics for the logic are that the rewrite cond directive is setting up a condition so that, if the condition is met, the rewrite rule will be executed.

It's a huge help, in the beginning (like me), to learn to read regular expressions, character by character, to understand what they're saying in "geek shorthand" and then to write what's needed to be done in plain English pseudocode for yourself.

So it isn't: return www.example.com if anything other than www.example.com is requested (that would also exclude forum.example.com), it's simply:

>>If the URI requested is anthing with example.com
>>Then return that request to the User Agent including the www. in front of the example.com part of the URI.

Logic for the forum:

>>If the URI requested is in /forum/ (remembering that it's all relative to the root of the domain for .htaccess)
>>Then serve up forum.example.com

Then, trying to write the directives is hugely helped by having a printed out list of the symbols and characters, and also a printed out paper with some examples that are similar to what you want to do, as a model.

Someone will correct me if and where I'm wrong, but this is what I'm finding helpful as I'm hobbling along.

Added:

Some helpful searches:

redirect subdirectory to subdomain (at WebmasterWorld) [google.com]

redirect index.html to www (at WebmasterWorld) [google.com]

Using variations in wording for additional searches will bring up even more threads on the topics.

[edited by: Marcia at 6:40 am (utc) on Dec. 10, 2007]

sfnet

7:51 am on Dec 10, 2007 (gmt 0)

10+ Year Member



Thank you Marcia.

In the meantime, I have decided to go with the following:

http://www.example.com/subfolder1/subfolder2/

Currently, I have learned how to rewrite the URL so that its formatted like so:

http://subfolder1.example.com/subfolder2/

OWNER EDIT (I was wrong, the following produces a loop I think)

Options -Indexes +FollowSymLinks 
RewriteEngine on
RewriteRule ^subfolder1/(.*) http://subfolder1.example.com/$1 [R,L]

When it is time, I will update the redirect to be a permanent redirect.

My next two items that I will work on are as follows:

1. As you pointed out:

write a directive so that requests for example.com without the www (example.com/)will return the www. root with a 301.

2. Remove index.php from a URL

I will continue to update this post as I go. I realize getting my end result will require the proper order of the rules.

[edited by: sfnet at 8:16 am (utc) on Dec. 10, 2007]

[edited by: jdMorgan at 10:21 pm (utc) on Dec. 10, 2007]
[edit reason] example.com [/edit]

sfnet

7:58 am on Dec 10, 2007 (gmt 0)

10+ Year Member



Getting closer, I decided I also wanted to rid my URLs of both index.php and .html and I was able to find this code:

Options -Indexes +FollowSymLinks 
RewriteEngine on
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.html\ HTTP/
RewriteRule ^(([^/]+/)*)index\.html$ http://%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.php\ HTTP/
RewriteRule ^(([^/]+/)*)index\.php$ http://%{HTTP_HOST}/$1 [R=301,L]

[edited by: jdMorgan at 10:21 pm (utc) on Dec. 10, 2007]

sfnet

8:46 am on Dec 10, 2007 (gmt 0)

10+ Year Member



Is this a proper way to "write a directive so that requests for example.com without the www (example.com/)will return the www. root with a 301":

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

Or is it the following take from your post here:
[webmasterworld.com...]

RewriteEngine On 
RewriteCond %{http_host} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

[edited by: jdMorgan at 10:22 pm (utc) on Dec. 10, 2007]
[edit reason] example.com [/edit]

sfnet

9:35 pm on Dec 10, 2007 (gmt 0)

10+ Year Member



And now we have a new one from the sitepoint blog:

Options +FollowSymlinks RewriteEngine on
RewriteCond %{HTTP_HOST} !^www¦subdomain1¦subdomain2\.example\.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=permanent,L]

This exempts specific subdomains. Seems like this could be the better choice. Maria, thoughts?

[edited by: jdMorgan at 10:23 pm (utc) on Dec. 10, 2007]
[edit reason] No URLs, please. See Terms of Service. [/edit]

jdMorgan

10:35 pm on Dec 10, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



All of these variations will work, but you'll need to pick one of them depending on what you want to do:
Some specifically redirect non-www to www, some redirect anything except www to www, and some redirect anything except a few specific subdomains to www.

Here is a completely-othodox example of the specific non-www to www redirect, with nothing missing and nothing extra -- by the book:


RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

Jim

sfnet

10:40 pm on Dec 10, 2007 (gmt 0)

10+ Year Member



Hi Jim,

Thank you very much.

I am stil learning a lot about how to write these statements.

My goal is to prevent two sites from being ranked in search engines.

When you begin the conditional with the ^ and there is no $ at the end, what is it doing? I am aware that ^ can mean two different things and I think the one most commonly used is to begin the regular expression.

jdMorgan

11:10 pm on Dec 10, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The ^ and $ are used as start- and end- anchors, respectively. A short example of patterns specifying an exact match, a match if the string begins with a value, a match if a string ends with a value, and a match if the string contains a value may be the simplest way to explain anchoring:

^exact-match$ (fully-anchored)
^begins-with (start-anchored)
ends-with$ (end-anchored)
contains (un-anchored)

You will often see "non-optimal" fully-anchored patterns posted on the Web such as "^example\.com.*$" -- This is utterly equivalent to the non-end-anchored pattern "^example\.com" except that it wastes the CPU's time processing three extra characters at the end that accomplish nothing.

You'll also often see unanchored patterns posted that leave the code open to malfunction due to unexpected/unforeseen matches.

Jim

sfnet

11:22 pm on Dec 10, 2007 (gmt 0)

10+ Year Member



Is it possible to take the output from those directives and put it through the "remove index.php" directive for example? I find that the rules work well independently from one another but when I begin to combine them, I get errors.

I suspect this is because I am not able to write or read the code well, and taking pieces of what other people write and expecting it to work properly is simply not possible. For that reason, I am really trying to learn this stuff.