Forum Moderators: phranque

Message Too Old, No Replies

htaccess needs updating

I need 4 directives on the htaccess file

         

Kaust

4:12 am on Sep 12, 2010 (gmt 0)

10+ Year Member



I am taking over web duties for my husband who is seriously ill and unable to help me. I only know basic html and am totally lost when it comes to updating the htaccess file.

The old htaccess file works but it needs updating because I have just figured out how to display my SSL on only one page of the site, the page in Wordpress that we give a price quotation on.

I need to accomplish 4 things:

1. example.com should go to www.example.com (This works in the current htaccess file)

2. certain very old pages (pre Wordpress) need to be redirected to their respective new page because, believe it or not, after many years, we are still finding incoming links to them. (This works in the current htaccess file)

3. A new page called /quotes/ needs to go to [example.com...] as that is the only page I want my SSL to display with https.

4. all other pages on the site need to be http and not https. I have found multiple samples of code to use to accomplish it but the code may as well be a foreign language. Most of the code I found refers to actual urls and sample code referred to them as "\NameOfFile\.php" which confused me because I need the url name to be "/quotes/" (and there are backward slashes -- pardon, my ignorance is showing!)

Current .htaccess actually works for 1, 2 and 4 listed above. If I try to do a redirect 301 for Item 3 with "redirect 301 /quote/ [example.com...] I go to my browser to test it, and my browser can’t get to it – I assume this is because there has been some continuous loop created in code above it, but I could be wrong. But, in code in htaccess file below, the /quickquote2.php actually does the redirect properly. Go figure.

Any help you can provide me to give me a jump start would be very much appreciated and I promise I'll spend good quality time trying to learn this myself! (Disclaimer: I have no idea what the "ErrorDocument" lines represent below, as I indicated, the htaccess file is old.

DirectoryIndex index.php index.html index.htm

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
redirect 301 /index.html http://www.example.com/
redirect 301 /homepage/ http://www.example.com/
redirect 301 /index.htm http://www.example.com/
redirect 301 /quote.php [example.com...]
ErrorDocument 403 /v-web/errdocs/403.html
ErrorDocument 401 /v-web/errdocs/401.html
ErrorDocument 500 /v-web/errdocs/500.html
ErrorDocument 400 /v-web/errdocs/400.html
ErrorDocument 404 /v-web/errdocs/404.html

jdMorgan

5:28 pm on Sep 13, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The mixed references to "quote" in your code and "quotes" in your your description are confusing. I have coded this to (hopefully) work correctly for both, assuming that the correct URL is "/quotes/" and the correct filepath to the physically-existing script file is "/quotes.php". Note that URLs and filepaths are not at all the same thing.

I have included your numbered "requirements list" entries in the comments. Please note that some appear more than once in cases where multiple rules are needed (and in the correct order) to fix multiple possible problems without causing multiple/chained/stacked redirects.

# Declare custom error document filepaths
ErrorDocument 403 /v-web/errdocs/403.html
ErrorDocument 401 /v-web/errdocs/401.html
ErrorDocument 500 /v-web/errdocs/500.html
ErrorDocument 400 /v-web/errdocs/400.html
ErrorDocument 404 /v-web/errdocs/404.html
#
# Enable mod_rewrite
RewriteEngine On
#
# 3. A new page called /quotes/ needs to go to https://www.example.com/quotes/ as
# that is the only page I want my SSL to display with https.
# Externally redirect direct client requests for /quote.php or /quotes.php or /quote/ to /quotes/
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /quote(/|s?\.php)([?#][^\ ]*)?\ HTTP/
RewriteRule ^quote(/|s?\.php)$ https://www.example.com/quotes/ [R=301,L]
#
# 3. A new page called /quotes/ needs to go to https://www.example.com/quotes/ as
# that is the only page I want my SSL to display with https.
# Externally redirect non-https requests for /quote/ or /quotes/ or /quote.php or /quotes.php to https
RewriteCond %{SERVER_PORT} !=443
RewriteRule ^quotes?(/|\.php)$ https://www.example.com/quotes/ [R=301,L]
#
# 4. all other pages on the site need to be http and not https
# Externally redirect all https requests for non-https pages back to http.
# (Note that all included-object types that are 'shared' between http and
# https pages are excluded, and that you may need to modify this list.)
RewriteCond %{SERVER_PORT} =443
RewriteCond $1 !\.(gif|jpe?g|png|css|js)$ [NC]
RewriteCond $1 !quotes(/|\.php)$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
#
# Externally redirect direct client requests for /index.htm or /index.html
# at any directory level to "/" at that same directory level
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*index\.html?([?#][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*)index\.html?$ http://www.example.com/$1 [R=301,L]
#
# 2. certain very old pages (pre Wordpress) need to be redirected to their respective new page
# Externally redirect requests for /homepage/ subdirectory to "/"
RewriteRule ^homepage(/.*)$ http://www.example.com$1 [R=301,L]
#
# 1. example.com should go to www.example.com (This works in the current htaccess file)
# Externally redirect all non-canonical hostname requests to canonical hostname
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

In all cases, the use of mod_rewrite directives instead of mod_alias (e.g. Redirect 301) directives and the order of the rules are critical to correct operation.

Jim