homepage Welcome to WebmasterWorld Guest from 54.161.200.144
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

This 77 message thread spans 3 pages: < < 77 ( 1 [2] 3 > >     
Canonicalization: best code to redirect no-www to www & index to /
Canonicalization: best code to redirect no-www to www & index to /
ewwatson




msg:4508253
 8:00 pm on Oct 15, 2012 (gmt 0)

Canonicalization: is this the best code to redirect no-www to www & index to root? I've read around the web for a couple days now and this is the best code I can find. Should I include Options +FollowSymLinks? And is this the leanest this can be to accomplish all it does? Thanks!

[size=2]Options +FollowSymLinks
RewriteEngine On
# redirect index.htm and index.html to /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.html?\ HTTP/
RewriteRule ^(.*)index\.html?$ http://www.mydomain.com/$1 [R=301,L]

# redirect no-www to www.
RewriteCond %{HTTP_HOST} ^mydomain\.com$ [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L][/size]

 

ewwatson




msg:4508321
 11:51 pm on Oct 15, 2012 (gmt 0)

Uh-oh I have a major problem. This htaccess is in the root of my top level domain. I have 4 other addon domains. Not subdomains. With this code in place when I go to the addon domain it forces it to read in the url http: //www (dot)topleveldomain(dot)com/addondomain(dot)com. Is there a fix for this? Because now that I see it the addon domains are essentially folders in root domain.

here is my exact code...

# Engine on only needed once
RewriteEngine On

# 301 permanent redirect index.html(htm) to folder
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.html?\ HTTP/
RewriteRule ^(([^/]+/)*)index\.html?$ http://www.example.com/$1 [R=301,L]

# 301 permanent redirect non-www (non-canonical) to www
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

g1smd




msg:4508326
 12:16 am on Oct 16, 2012 (gmt 0)

Add exclusions for those hostnames to both rules.

RewriteCond %{HTTP_HOST} !(this|that|other)...

Start anchor not needed, no need to specify www\. within; just match the hostname and TLD.

ewwatson




msg:4508328
 12:39 am on Oct 16, 2012 (gmt 0)

Can you show me what that would look like please? Or I could just put a htaccess in each subfolder in this root domain to redirect the indexes. In which case is this all thats needed for the index redirect to folder or am I totally wrong again?

RewriteRule ^index\.html$ http://www.example.com/ [L,R=301]

This one seems to work well but completely omits the RewriteCond line. Im sure its wrong. Mind showing me how to improve that simple code as well?

ewwatson




msg:4508331
 1:19 am on Oct 16, 2012 (gmt 0)

Ok I finally found one. They are not easy to find on google or this search. From jdMorgan. This was posted in 2005. Any updates to this one? If I go this route I will just put this in each sub folder with a index.

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html\ HTTP/
RewriteRule ^index\.html$ http://www.example.com/ [R=301,L]

lucy24




msg:4508335
 2:29 am on Oct 16, 2012 (gmt 0)

I have 4 other addon domains. Not subdomains. With this code in place when I go to the addon domain it forces it to read in the url http: //www (dot)topleveldomain(dot)com/addondomain(dot)com

Careful. Are you talking about URLs or about physical location? I guess you're on shared hosting with multiple domains. The usual pattern is

/users/yourname/example.com
/users/yourname/example.org
/users/yourname/example.net

which lets you make two layers of htaccess files: a shared one for things like Deny From that apply across the board, and then one for each domain.

Have you instead got

/users/yourname/example.com
/users/yourname/example.com/example.org
/users/yourname/example.com/example.net

like that? Eeuw. Too late for you-- but for anyone else reading this: if you know beforehand that your host uses this configuration, start with a nothing domain that you won't even try to use, and put all the real stuff in the addons. You need all your domains to be parallel.

Can you show me what that would look like please?

The point is that requests for all the other domains will pass through the first domain's htaccess. (Assuming I've understood you right.) So anything that's specific to the first domain will always have to include a line that says

RewriteCond %{HTTP_HOST} !(other1|other2|other3)

You don't need to spell out the full names or use any anchors-- just say enough to distinguish them from the first domain. F'rinstance if you really did have dot com, dot org and dot net, then any rule that should apply only to dot com would be preceded by

RewriteCond %{HTTP_HOST} !\.(org|net)

You only need to do this when there is a chance of ambiguity, as with "index.html" redirects. If it's a specific filename that only exists in the first domain, you don't need any extra conditions.

ewwatson




msg:4508339
 3:22 am on Oct 16, 2012 (gmt 0)

In my public_html I have...

myMainDomain.com/2addOnDomain.com
myMainDomain.com/3addOnDomain.com
myMainDomain.com/4addOnDomain.com

Does that make sense? I have Hostgator shared. So when I add a domain it just makes another folder in my root. I am open to do either way. Either exclude those directories or just use separate htaccess. Which ever is better, less hacky, cleaner, and easier to remember 5 years from now. I don't know much about this htaccess code so ill take your guys advice and go with it. They are all .com's. So please show me exactly how I would exclude 3 other directories (ie websites)? If I try I'm only guessing. As I can see this code needs to be exact to work correctly.

g1smd




msg:4508365
 6:23 am on Oct 16, 2012 (gmt 0)

You don't need any of the code in #4508328 or #4508331. We've got the code for index redirects and canonicalisation redirects sewn up many posts back. Use the same code for each domain, editing it for the correct domain name, and then place the code in the right place for it to work.

You'll need the two rulesets for the main domain in the main domain root folder. You'll need the two rulesets for each add-on domain in each of the add-on domain folders.

The root htaccess rule for the main domain needs extra exclusions on both rulesets so as to not redirect any of the add-on domains.

Each add-on-domain folder needs its own htaccess rules to deal with index and non-www but only for the add-on-domain.

# Redirect index.html, .htm and .php to folder
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.(html?|php)\ HTTP/
RewriteRule ^(([^/]+/)*)index\.(html?|php)$ http://www.example.com/$1 [R=301,L]


# Redirect non-canonical to www
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]


These rules will all follow the same format, one for each domain, except for the main domain having the extra exclusions before both rulesets:

RewriteCond %{HTTP_HOST} !(example\.net|example\.org|example\.co\.uk)
ewwatson




msg:4508383
 9:00 am on Oct 16, 2012 (gmt 0)

Ok well explained. Thank you! For good measure I'm going to show here how I interpreted what you said. Is this correct? And can I assume I can safely do away with the php part in the main domain (only has html extensions) as long as I include php in the addon domain code (they have php extensions)?

Main domain...

# Engine on only needed once
RewriteEngine On

# Redirect index.html, .htm and .php to folder
RewriteCond %{HTTP_HOST} !(example\.net|example\.org|example\.co\.uk)
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.(html?|php)\ HTTP/
RewriteRule ^(([^/]+/)*)index\.(html?|php)$ http://www.example.com/$1 [R=301,L]

# Redirect non-canonical to www
RewriteCond %{HTTP_HOST} !(example\.net|example\.org|example\.co\.uk)
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

and Addon domains...

# Engine on only needed once
RewriteEngine On

# Redirect index.html, .htm and .php to folder
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.(html?|php)\ HTTP/
RewriteRule ^(([^/]+/)*)index\.(html?|php)$ http://www.example.com/$1 [R=301,L]

# Redirect non-canonical to www
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

ewwatson




msg:4508455
 1:53 pm on Oct 16, 2012 (gmt 0)

Also, with this code in place if I run my url through this redirect checker tool [ragepank.com...] they ALL come back as 301 permanent redirects. I dont get one response 200. Shouldn't I get one? Can anyone explain why? I'm guessing only http://www.example.com/ comes back as ok 200 but why didnt it list it?

below his checker tool he says... "Generally speaking, only one of these should return a 200 response, and this is usually www.example.com"

lucy24




msg:4508534
 6:37 pm on Oct 16, 2012 (gmt 0)

You're better off checking with your own eyeballs using something like Firefox's Live Headers, or equivalent for other browsers. Request any given name and you should see what happens. (In the case of FF you will also see an appalling number of side requests to google-related functions. Later on, you can have fun fine-tooth-combing the browser prefs to make sure they are only doing what you want and nothing that you don't.)

Each individual domain should give you one 200 version and one or more 301 versions, depending on how many variations you try.

Edit: The link you gave-- which may or may not survive the next moderator visit-- gave me a steady stream of 403's, illustrating something I commented on only yesterday in an unrelated thread :) The tool lives in 174.133, ThePlanet; I'd have to unblock it for the tool to work. Except that you don't need to, because they thoughtfully show you the list of forms they try. With and without www, with assorted extensions, with and without "index.something". Good list, in principle.

[edited by: lucy24 at 6:44 pm (utc) on Oct 16, 2012]

g1smd




msg:4508541
 6:43 pm on Oct 16, 2012 (gmt 0)

I assume I can safely do away with the php part in the main domain

You could, but it is better to use the same code (barring domain name changes) for every site.
Code is easier to maintain if there are less variations.

lucy24




msg:4508544
 6:57 pm on Oct 16, 2012 (gmt 0)

Follow-up after temporarily enabling the tool (whose UA incidentally is a confidence-inspiring "Snoopy v1.2.3"):

They're wrong. On two domains they reported 200 responses for both with and without www, although one of the two is supposed to be only without and the other is only with. I was all set to swear at my host-- I've set it as a pref in their control panel-- but tried it by hand first. And got correctly redirected in each case. Logs confirm 301 for the "wrong" version.

? ? ?

I suggest you look at your own logs and see if they match the reported results.

ewwatson




msg:4508560
 7:47 pm on Oct 16, 2012 (gmt 0)

You guys are very helpful. :) I just realized I do have php extensions on my formmail.php and formmailerror.php. There's no strange htaccess vudo that I'm not thinking of that could throw a problem with those is there if I'm only using HTML in the rule sets on my main domain?

Lol my repeated questions probably stems from the fact that I am not comfortable playing with code I don't understand. Specially code like this that can break my site. With CSS I'm like you guys. I look at big string of code and can follow it in my head 20 steps out. Frustrating. I'm used to being on the other end of the questions.

g1smd




msg:4508562
 7:57 pm on Oct 16, 2012 (gmt 0)

If you're only redirecting requests for index.php, then the rule cannot interfere with other .php requests.

If you were redirecting all .php requests to some sort of extensionless URL, then you could either exclude formmail requests or exclude POST requests.

There's more than one way to do this, and the right way to do it will differ for different sites. There's no one-size-fits-all cut-and-paste solution to be had.

ewwatson




msg:4535356
 9:08 pm on Jan 11, 2013 (gmt 0)

I'm back! New issue with same code. If I don't have a index file in one of the addon domains it redirects to the main domain. Is this normal behavior for the below htacces code? Or is something amiss? Thanks

# 301 permanent redirect index.html(htm) to folder with exclusion for addon domains
RewriteCond %{HTTP_HOST} !(addondomain\.com|addondomain\.com|addondomain\.com|addondomain\.com|addondomain\.com)
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.html?\ HTTP/
RewriteRule ^(([^/]+/)*)index\.html?$ ht tp://w ww.maindomain.com/$1 [R=301,L]

# 301 permanent redirect non-www (non-canonical) to www with exclusion for addon domains
RewriteCond %{HTTP_HOST} !(addondomain\.com|addondomain\.com|addondomain\.com|addondomain\.com|websitecodetutorials\.com)
RewriteCond %{HTTP_HOST} !^(www\.maindomain\.com)?$
RewriteRule (.*) ht tp://w ww.maindomain.com/$1 [R=301,L]

Note I put spaces in the main domain - I forgot the domain to use to bypass that.

ewwatson




msg:4535562
 11:18 pm on Jan 12, 2013 (gmt 0)

Anybody know?

lucy24




msg:4535580
 2:41 am on Jan 13, 2013 (gmt 0)

:: returning after Three Separate Attempts to ignore the question in hopes that someone else will answer it ::

The domain name you're groping for is example dot com. It's in, ahem, the Forum charter and also the Sticky at the top of the Apache forum list.

If I don't have a index file in one of the addon domains it redirects to the main domain. Is this normal behavior for the below htacces code?

Iffy wording there. Do you mean that you want it to do this and it doesn't, or that it's doing it and you want it to stop?

Are your add-ons completely different domain names or are they subdomains? (Got a vague idea "add-on" is a technical term used by some hosts but I, uhm, forgot what-if-anything it means.)

Assuming for the sake of discussion that #1 they're separate domain names, #2 the "add-ons" and your "main" domain all pass through the same htaccess, #3 there's no risk of overlap among domain names ...

Your code as given doesn't say anything about whether "index\.html?" exists-- either in reality or in potentia*-- or not.

First rule: Kicks in if request is sent to anything other than the list of named domains. I don't know-- and htaccess doesn't care-- whether this applies to one domain or hundreds. If the user's request ends in "index.htm(l)" then capture the directory name, if any, and redirect to the same named directory in one specific domain.

This is already confusing. Do all your domains have the same URL structure, so requests for

example-two.com/directory/index.html
www.example-six.com/directory/subdirectory/index.html
example-eight.com/directory/subdirectory/subsubdirectory/index.html

will be successfully redirected to

www.example.com/directory/
www.example.com/directory/subdirectory/
www.example.com/directory/subsubdirectory/

respectively?

Now, the second rule makes me suspect that you've got a finite number of add-ons and you're excluding all of them-- both here and in the preceding rule-- although the htaccess doesn't say so.

In the second case, it seems like it would be safer to say

RewriteCond %{HTTP_HOST} example\.com
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule et cetera

and then you don't have to adjust the htaccess every time you add another domain. If the request is for anything other than example dot com, Condition 1 fails and the rule is ignored.

If Rule 1 is intended to do the same thing-- that is, it only applies to one specific domain-- use the same pair of conditions. But that's where we get into the confusion of wording. The # comment in the htaccess itself doesn't seem to say the same thing as your post. :(


* That is: if you've got auto-indexing enabled for a particular directory, the index file may not exist beforehand, but it's created and shown to the user as if it had been there all along. Except that a "real" index file probably wouldn't be an HTML 3 table ;)

ewwatson




msg:4535609
 4:55 am on Jan 13, 2013 (gmt 0)

Thank you for the responce. i know its one of those non gradifing questions to answer. Lol I had a hard time following you there. No I definitely do not want it to redirect to the addon domain. So yes I would like it to stop. Like it to NOT redirect to main/addon if there is no index file on the addon that is. The addon domains are fully independent domains. It's what Hostgator calls it when you add on new domains. FYI they are not sub domains. I believe they are all very similar in folder structure.

Making it so I don't have to change the htacces every time I add a domain sounds good to me. And making it so I don't need a index file in the addon. Anyway you can add that to my for mentioned code so I don't mess it up? Thank you!

lucy24




msg:4535616
 6:16 am on Jan 13, 2013 (gmt 0)

Like it to NOT redirect to main/addon if there is no index file on the addon that is.

Well, that's the tricky bit. When you say "there is no index file" are you talking about physical files that either exist or don't exist? Your rule as written does something entirely different: It redirects any requests ending in "index.html" to the equivalent directory minus "index.html" -- but it also sends them all over to the main domain unless they're on the excluded list.

Now, you are about to hear a Voice of Dissent
:: twiddling thumbs and avoiding eye contact with g1 ::
but this is one of those situations where it's a ### of a lot easier if each domain has its own htaccess. Is this something your host won't let you do?

What's the physical directory structure? Are the add-ons in directories inside the first domain's directory, or are they all side by side in a single userspace? (The second way is the configuration that I'm used to. The first way is more common with subdomains.)

There's nothing in the existing code that would redirect to the primary domain when the request doesn't end in index.html.

Then again, I may be completely misunderstanding the question and your real problem is that the second rule kicks in when it wasn't supposed to. Or...

... or are both rules a red herring, and what's happening is that something that ought to serve a 403 or 404 in an add-on domain is instead sending people to the first domain?

How 'bout some real-life examples? You request A; you want or expect to end up at B; but instead you find yourself at C.

Note that B and C are each two different things: The content of the address bar and the content-- including title-- of the page. Since they are your sites, you know whether each of these pairs are matching up they way you want them to.

Edit: Oh, wait, I'm overlooking the obvious question. Why do you not want the add-ons to have index files? Where do humans end up if they request

www.example-two.com

and so on? Normally it would be the index page. Unless you're talking about domains that don't really exist at all; you're just using the names as aliases for the "real" domain. In which case you may be able to cop out, click some buttons and let the host handle it ;)

ewwatson




msg:4535654
 12:13 pm on Jan 13, 2013 (gmt 0)

Good morning,

99% of the time it's a moot thing I guess as I will always have a index in the root. The only reason I noticed it was because I just started developing it and took it live with no files. I was mainly just asking if this is normal behavior? I guees it is right? It redirects to folder if no index. I just figured it would not travel back down the folders looking for the first index.

The structure is like this... /public_html/maindomain.com/addondomain.com - the same for each addon.

Each addon domain is fully functional. They do already have their own htaccess.

Here is what I have in my addon domain htaccess...

# 301 permanent redirect index.php to folder
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.php\ HTTP/
RewriteRule ^(([^/]+/)*)index\.php$ http://www.exampledomain.com/$1 [R=301,L]

# 301 permanent redirect non-www (non-canonical) to www
RewriteCond %{HTTP_HOST} !^(www\.exampledomain\.com)?$
RewriteRule (.*) http://www.exampledomain.com/$1 [R=301,L]


Thanks

ewwatson




msg:4535667
 2:46 pm on Jan 13, 2013 (gmt 0)

Because I was originally getting no responce here I started a new thread over at sitepoint. I trust your guys opinion since you seem to know your stuff. A veteran over there gave me this solution. What do you think? Is it perfect or is it missing things? I know little to nothing about this stuff so I am at your kind mercy.

RewriteEngine on
# Send subdomains off to their own domains so
# you don't need to worry about exclusions below
RewriteCond %{HTTP_HOST} ^((www\.)?([a-z]+\.))maindomain\.com$ [NC]
# Capture optional www and subdomain (better to list your subdomains inside (sub1|sub2|...) )
RewriteRule .? http://%1com{%REQUEST_URI} [R=301,L]
# Send {REQUEST_URI} to subdomain.com
# Note, this can also send to www.maindomain.com
# (if you don't list as recommended above) but
# that will be corrected below

# 301 permanent redirect index.html(htm) to folder with exclusion for addon domains
# Rephrased: Strip subdirectories from any index.htm(l) request
# RewriteCond %{HTTP_HOST} !(addondomain\.com|addondomain\.com|addondomain\.com|addondomain\.com|addondomain\.com)
# Irrelevant
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.html?\ HTTP/
# {THE_REQUEST} is a terrible variable to use and you don't need to use the whole thing!
# In fact, you don't need to use it at all!
RewriteRule .+/index\.html?$ http://www.maindomain.com/index.html [R=301,L]

# 301 permanent redirect non-www (non-canonical) to www with exclusion for addon domains
RewriteCond %{HTTP_HOST} !(addondomain\.com|addondomain\.com|addondomain\.com|addondomain\.com|websitecodetutorials\.com)
# Ditto the above
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule .? http://www.maindomain.com${REQUEST_URI} [R=301,L]

lucy24




msg:4535799
 2:31 am on Jan 14, 2013 (gmt 0)

The first quoted rule is about subdomains. I thought we were talking about independent domain names :(
The only reason I noticed it was because I just started developing it and took it live with no files. I was mainly just asking if this is normal behavior? I guess it is right? It redirects to folder if no index.

I'm now inclined to say that I was right-- by accident at least-- and the whole question really is a red herring. If you're got live domains with no content, and people are getting redirected to your primary domain, that sounds very much like something the host is doing. Nothing to do with your htaccess at all. The alternative remedy in shared hosting is to populate each named domain with a placeholder page, which will be ignored if the site has an appropriately named index file. (As far as I can tell, it's a real physical file. It doesn't disappear unless I manually delete it.) The config file probably has something like "placeholder.html" as the last item in the DirectoryIndex list.

helenp




msg:4536037
 11:20 pm on Jan 14, 2013 (gmt 0)

# Redirect index.html and .htm to folder
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.html?\ HTTP/
RewriteRule ^(([^/]+/)*)index\.html?$ http://www.example.com/$1 [R=301,L]

# Redirect non-canonical to www
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

I just tried this code (G1smd's)
However it made my https not to work in chrome....

g1smd




msg:4536043
 12:01 am on Jan 15, 2013 (gmt 0)

Correct. That code redirects everything to http and www.

If you have part of a site using https, then you'll need another rule for that, redirecting to https.

To make it all work, both of the rules will need extra preceding conditions: one testing the requested port is ^443$ or !^443$ and the other testing the requested path and file that should be http or should be https.

lucy24




msg:4536071
 2:18 am on Jan 15, 2013 (gmt 0)

If you have part of a site using https, then you'll need another rule for that, redirecting to https.

That's happening in the thread next door [webmasterworld.com]. By the time you hit page three it's solidly in How-To territory, but I think the mods were afraid of snipping for fear of severing some vital artery. :)

helenp




msg:4536130
 9:33 am on Jan 15, 2013 (gmt 0)

Tried to mix G1Smd's code with TheMadScientists code to fit my needs but redirects index.htm on third level (mysite/file/file/index.htm) from http to https
Correct. That code redirects everything to http and www.

If you have part of a site using https, then you'll need another rule for that, redirecting to https.

To make it all work, both of the rules will need extra preceding conditions: one testing the requested port is ^443$ or !^443$ and the other testing the requested path and file that should be http or should be https.

I tried this:
# Redirect index.html and .htm to folder
RewriteRule !index\.html? - [S=2]
RewriteCond %{SERVER_PORT} !^443$
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.html?\ HTTP/
RewriteRule ^(([^/]+/)*)index\.html?$ http://www.example.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.html?\ HTTP/
RewriteRule ^(([^/]+/)*)index\.html?$ https://www.example.com/$1 [R=301,L]


# Redirect non-canonical to www
RewriteCond %{SERVER_PORT} !^443$
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) https://www.example.com/$1 [R=301,L]

g1smd




msg:4536133
 9:48 am on Jan 15, 2013 (gmt 0)

Conditions apply only to the next rule that follows.

You need the port test in some form or other ("is 443" or "is not 443") before every rule.

Add a blank line after each rule to make the code more readable.

helenp




msg:4536141
 10:26 am on Jan 15, 2013 (gmt 0)

Conditions apply only to the next rule that follows.

You need the port test in some form or other ("is 443" or "is not 443") before every rule.

Add a blank line after each rule to make the code more readable.


Puf, I have changed it to this and it gives the same result, redirects http to https on the third level when writing index.htm
# Redirect index.html and .htm to folder
RewriteRule !index\.html? - [S=2]
RewriteCond %{SERVER_PORT} !^443$
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.html?\ HTTP/
RewriteRule ^(([^/]+/)*)index\.html?$ http://www.example.com/$1 [R=301,L]

RewriteCond %{SERVER_PORT} ^443$
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.html?\ HTTP/
RewriteRule ^(([^/]+/)*)index\.html?$ https://www.example.com/$1 [R=301,L]

# Redirect non-canonical to www
RewriteCond %{SERVER_PORT} !^443$
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

RewriteCond %{SERVER_PORT} ^443$
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) https://www.example.com/$1 [R=301,L]

g1smd




msg:4536155
 11:24 am on Jan 15, 2013 (gmt 0)

There must be some other rule elsewhere that conflicts.

Do we need this line?
RewriteRule !index\.html? - [S=2]

dr0832




msg:4536257
 6:04 pm on Jan 15, 2013 (gmt 0)

What part of this needs to be change. I assume I just need to fill in my domain instead of example? Sorry this is pretty far over my head but I am trying to secure my website.


# Redirect index.html and .htm to folder
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.html?\ HTTP/
RewriteRule ^(([^/]+/)*)index\.html?$ http://www.example.com/$1 [R=301,L]

# Redirect non-canonical to www
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

helenp




msg:4536259
 6:09 pm on Jan 15, 2013 (gmt 0)

There must be some other rule elsewhere that conflicts.

Do we need this line?
RewriteRule !index\.html? - [S=2]


I have tried both yours and themadscientist, both with and without that line, and sometimes it worked then suddenly I got a https file when I wrote index.htm in an http adress.
So it looks like there is some strange cache issue when swithch form https to http
however it works, thanks

This 77 message thread spans 3 pages: < < 77 ( 1 [2] 3 > >
Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved