Forum Moderators: phranque
From with ISPs when people put this in their address bar and press enter or go, nothing happens.
Mydomain.com
No error message; no page loading … nothing! It just freezes. In this case they can still access other sites with out typing the [www....] Part. I.e. they can access yahoo by just typing yahoo.com in address bar and pressing go.
In this case they can only access my site by adding the http part. like this:
http://www.mydomain.com
I couldn’t believe it my self until I tried accessing my newly reached site from a coffee net.
I have this in my .htaccess file. Am I missing something there?
RewriteEngine OnRewriteBase /
RewriteCond %{REQUEST_FILENAME}!-f
RewriteCond %{REQUEST_FILENAME}!-d
RewriteRule ^(.*)$ index.php/$1 [L]
RewriteCond %{REQUEST_URI}\\/%{HTTP_HOST}/www. ^/+(.+/)?[^.]*[^/]\\(/)([^w][^w][^w][^.].*/(www\.)¦.*)$ [OR,NC]
RewriteCond %{HTTP_HOST}/www. ^(/)?(/)?([^w][^w][^w][^.].*/(www\.))$ [NC]
RewriteRule ^ http://%4%{HTTP_HOST}%{REQUEST_URI}%2 [L,R=301]
DirectoryIndex index.php index.shtml index.html index.htm
Thank you
[edited by: jdMorgan at 12:36 am (utc) on Feb. 21, 2006]
[edit reason] De-linked [/edit]
RewriteCond %{REQUEST_URI}\\/%{HTTP_HOST}/www. ^/+(.+/)?[^.]*[^/]\\(/)([^w][^w][^w][^.].*/(www\.)¦.*)$ [OR,NC]
RewriteCond %{HTTP_HOST}/www. ^(/)?(/)?([^w][^w][^w][^.].*/(www\.))$ [NC]
RewriteRule ^ http://%4%{HTTP_HOST}%{REQUEST_URI}%2 [L,R=301]
Jim
RewriteEngine on
RewriteBase /
#
# Add trailing slash if missing (skip rewrite for paths with
# a period the last part, already ending in slash, or blank)
RewriteCond %{REQUEST_URI} !(\.[^/]+¦/)$
RewriteRule (.+) /$1/
#
# Redirect non-www domains to www domains w/same file
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule (.*) http:www.%{HTTP_HOST}/$1 [R=301,L]
#
# If requested resource does not exist as a file or directory, rewrite to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php/$1 [L]
Jim
I also noticed a couple of typos in the code I posted above. THis might work better:
RewriteEngine on
RewriteBase /
#
# Add trailing slash if missing (skip rewrite for paths with
# a period the last part, already ending in slash, or blank)
RewriteCond %{REQUEST_URI} !(\.[^/]+¦/)$
RewriteRule (.+) /$1/
#
# Redirect non-www domains to www domains w/same file
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule (.*) http:[b]//ww[/b]w.%{HTTP_HOST}/$1 [R=301,L]
#
# If requested resource does not exist as a file or directory, rewrite to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) [b]/ind[/b]ex.php/$1 [L]
This does work:
RewriteEngine on
RewriteBase /
#
# Add trailing slash if missing (skip rewrite for paths with
# a period the last part, already ending in slash, or blank)
RewriteCond %{REQUEST_URI}!(\.[^/]+¦/)$
RewriteRule (.+) /$1/
#
# Redirect non-www domains to www domains w/same file
RewriteCond %{HTTP_HOST}!^www\.
RewriteRule (.*) [%{HTTP_HOST}...] [R=301,L]
#
HOWEVER, I am using OSCOMMERCE.
I can change it so the domain always starts with a [www....] but the links and images inside the shopping cart program still look like this:
[url.com...]
Note the www. missing from the graphics and links inside the pages.
Is there a way to rewrite the links and images links?
Shane
If OSC uses a system variable to build URLs, you could look into changing that variable in your config file, thus effecting a system-wide change from non-www to www.
For example, if your ServerName is example.com and there is a ServerAlias for www.example.com, then reversing those two values in httpd.conf might fix the problem. See also USeCanonicalDomain -- although I'm not sure that setting would change anything realted to OSC.
Another alternative would be to use the non-www domain as your canonical domain name by reversing the non-www to www redirect posted above. This would require search engines to change all their listings from www to non-www, a process that would take 30 days or more, and it would also be a good idea to ask your major link sources to change them as well.
mod_rewrite works as requests are received at your server, before any content-handlers or scripts are invoked. Therefore, it cannot be used to change links that are published on your pages.
Jim
Normally, it goes like this:
http://example.com/subdir <== Input
http://example.com/subdir/ <== Redirect by Apache
http://www.example.com/subdir/ <== Redirect for adding www
With the code, it goes like this:
http://example.com/subdir <== Input
http://www.example.com/subdir/ <== Redirect for adding www and trailing slash
But the code must be placed after "SEO friendly/ShortURL' code, in general.
And it's not compatible with some sites that uses filename without extension.
Same thing for the Jim's code.
I mean, the trailing slash code should be placed after the OSc, Drupal, whatever using the rewriteRule, most probably.
RewriteEngine on
RewriteBase /
#
# If requested resource does not exist as a file or directory, rewrite to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /index.php/$1 [L]
#
# Add trailing slash if missing (skip rewrite for paths with
# a period the last part, already ending in slash, or blank)
RewriteCond %{REQUEST_URI} !(\.[^/]+¦/)$
RewriteRule (.+) /$1/
#
# Redirect non-www domains to www domains w/same file
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule (.*) [%{HTTP_HOST}...] [R=301,L]
If you don't have access to error.log, you can use small cgi/php to check what you are getting.
env.cgi
#!/bin/sh
echo
printenv¦sort
Then add a RewriteRule for testing.
RewriteCond %{QUERY_STRING} ^zzz
RewriteRule !^/*env.cgi env.cgi [L]
Type "example.com/?zzz" in the browser, and you should see all env variables for you to study the situation.
If you prefer PHP: env.php
[php]
<pre><? print_r($_SERVER);?>
[/php]
RewriteCond %{QUERY_STRING} ^zzz
RewriteRule !^/*env.php env.php [L]
As it requires QUERY_STRING to be started with "zzz" (or any other string you prefer), normal requests are not affected unless your site uses parameter named "zzz".
I was talking about extras's last post.
RewriteEngine on
RewriteBase /
#
# If requested resource does not exist as a file or directory, rewrite to index.php
RewriteCond %{REQUEST_FILENAME}!-f
RewriteCond %{REQUEST_FILENAME}!-d
RewriteRule (.*) /index.php/$1 [L]
#
# Add trailing slash if missing (skip rewrite for paths with
# a period the last part, already ending in slash, or blank)
RewriteCond %{REQUEST_URI}!(\.[^/]+¦/)$
RewriteRule (.+) /$1/
#
# Redirect non-www domains to www domains w/same file
RewriteCond %{HTTP_HOST}!^www\.
RewriteRule (.*) [%{HTTP_HOST}...] [R=301,L]
Shoild the [L] be there in the first rule?
If you do this script rewrite step before the domain canonicalization redirect and you don't use an [L] flag on it, then you will expose your script's URL to users and search engines, and it will get listed in search engines -- not good. If you do use an [L] flag, then the domain won't be canonicalized for requests handled by the script. So that code order is just not right.
Jim
I can't tell what's wrong without seeing your server error log. Bad Request means that the browser sent a bad request to your server. It's possible that this was the result of a redirection loop, but it's hard to tell.
Maybe it's this line, missing two slashes:
RewriteRule (.*) http:www.%{HTTP_HOST}/$1 [R=301,L]
Should be:
RewriteRule (.*) [%{HTTP_HOST}...] [R=301,L]
And this important reminder, already posted above:
Note that you must change the broken pipe "¦" character to a solid pipe character before use.
Jim
-------------------------------------
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI}!(\.[^/]+¦/)$
RewriteRule (.+) /$1/
RewriteCond %{HTTP_HOST}!^www\.
RewriteRule (.*) [%{HTTP_HOST}...] [R=301,L]
RewriteCond %{REQUEST_FILENAME}!-f
RewriteCond %{REQUEST_FILENAME}!-d
RewriteRule (.*) /index.php/$1 [L]
I suspect that your serve is misconfigured, or that some other code in httpd.conf or another .htaccess file is interfering with the rewrites.
Jim
They gave me this instead but this is not adding the trailing slash to urls.
----------------------------
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI}!(.[^/]+¦/)$
RewriteRule (.+) /$1/
RewriteCond %{HTTP_HOST}!^www.
RewriteRule (.*) [%{HTTP_HOST}...] [R=301,L]
RewriteCond %{REQUEST_FILENAME}!-f
RewriteCond %{REQUEST_FILENAME}!-d
RewriteRule (.*) index.php?$1 [L]
----------------------------
I will appreciate any comment.
Thank you
How kind of them, since they only changed two characters, and one of their changes is incorrect... :)
This line has been changed, and will prevent the trailing slash fix from working:
RewriteCond %{REQUEST_URI} !(.[^/]+¦/)$
RewriteRule (.*) index.php?$1 [L]
RewriteEngine on
RewriteBase /
#
# Add trailing slash if trailing slash is not present and
# URI does not end with a period and filetype (e.g. .gif)
RewriteCond %{REQUEST_URI} [b]!(\.[/b][^/]+¦/)$
RewriteRule (.+) /$1/
#
# Redirect to canonical doamin if non-canonical domain is
# requested (prepend "www" to requested hostname)
RewriteCond %{HTTP_HOST} !^www.
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L]
#
# If requested resource does not exist as a file or directory,
# rewrite to index.php with requested path as query parameter
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?$1 [L]
Jim