Forum Moderators: phranque
Simple application - single rewrite rule - works when in the httpd.conf file but NOT in the .htaccess file. Here is the rule:
RewriteRule ^/$ http://example.com/ht_test/ [L] I have performed the following to make sure that the .htaccess should work fine:
1) Verified that "AllowOverride All" is set for my <Directory> in question
2) Verified that the .htaccess file is being read - used a "garbage" test to verify file read.
3) Verified that the .htaccess pattern is being applied - checked this is the mod logs.
4) Verified that NO OTHER CONF files are being applied that could overwrite my <Directory> Settings.
Again, when the above rule is in my <VirtualHost> section, it works great. As soon as I comment those out and move them to the .htaccess file, all I get is the standard index.php file instead of the redirected version.
Ideas please - Thanks.
[edited by: jdMorgan at 4:50 pm (utc) on April 19, 2008]
[edit reason] example.com [/edit]
What am I missing here? Is the "/" present in the URI for the .conf file and not in the per directory checking?
Thanks by the way...hair is growing back now...
Yes, you use the complete URL-path in code in httpd.conf or conf.d, etc. In .htaccess, you must remove that part of the URL-path that was used to get to the current .htaccess directory from the RewriteRule pattern. In other words, in a per-directory context, the pattern must be "localized" to the directory in which the .htaccess file is located. This is documented in the Apache mod_rewrite docs -- in the notes following the description of the RewriteRule directive.
Example to internally rewrite /foo/bar/widget.html to /foo/bar/widget.php, use:
In httpd.conf : RewriteRule ^/foo/bar/widget\.html$ /foo/bar/widget.php [L]
In /.htaccess : RewriteRule ^foo/bar/widget\.html$ /foo/bar/widget.php [L]
In /foo/.htaccess : RewriteRule ^bar/widget\.html$ /foo/bar/widget.php [L]
In /foo/bar/.htaccess : RewriteRule ^widget\.html$ /foo/bar/widget.php [L]
Jim
1) Are they used just to reference back to content contained within parens?
2) How do the numbers work? For example, when I see "$3" or "%2", what are these referencing?
Thanks again...
Where parenthesized sub-expressions are nested, count left parentheses to determine the back-reference number.
You may use $1 through $9 and/or %1 through %9 -- all other values are undefined. Rules requiring more than nine backreferences of either type must be broken down into several steps using multiple RewritRules and/or RewriteConds.
If you read the documentation strictly rather than liberally, you should be trouble-free. That is, if the documentation says it won't work, or does not say that it will work, then it probably won't work.
Jim
The first occurrence of "%2" is especially confusing because I would expect to find 2 sets of "(SOME_TEXT)" in one of the preceding lines but as you can see, it is not there...Last question - I promise...
# Fix missing trailing slashes.
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.domain\.com$ [NC]
RewriteCond %{DOCUMENT_ROOT}/%2%{REQUEST_URI}/ -d
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]
# Rewrite sub domains.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.domain\.com$ [NC]
RewriteRule ^(.*)$ /userpages/%2/$1 [QSA,L]
In order to canonicalize the hostname to avoid duplicate-content problems and dilution of PageRank and Link-popularity, I'd suggest changing that first rule to:
RewriteRule [^/]$ http://%2/example.com%{REQUEST_URI}/ [R=301,L]
# canonicalize hostname; remove leading "www" from subdomain & remove any trailing "." or port numbers
RewriteCond %{HTTP_HOST} !^([^\.]+)\.example\.com$
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.example\.com
RewriteRule (.*) http://%2.example.com/$1 [R=301,L]
Note that the path being checked for directory-exists in your first rule is inconsistent with the path in your second rule. If these rules are intended to work together, then the third RewriteCond in the first ruleset should probably also include the "/userpages" path-part, unless that path-part is already defined as part of DocumentRoot:
RewriteCond %{DOCUMENT_ROOT}/userpages/%2%{REQUEST_URI}/ -d
So, for example, if your home page URL resolves to a file named "index.php", then
http://example.com.:80/index.php, which is a perfectly-valid URL, should be redirected to
http://www.example.com/
to avoid trouble with search engine indexing and ranking.
Have fun!
Jim