Reputation: 35284
Everywhere I look online the htaccess tutorials are all pretty basic. None of them explain what
RewriteCond %{HTTP_HOST} ^www\.mysite\.com$ [NC]
RewriteRule ^(.*)$ http://mysite.com/$1 [R=301,L]
[NC]
and [R=301,L]
mean (I can figure the 301 redir tho)
Also where did the %{HTTP_HOST}
token come in? What are some others? When was $1
captured? ...
Are there any tutorials that explain all of this?
EDIT: Here's my htaccess file for mguymon:
Options -Indexes
ErrorDocument 404 /404.php
CheckSpelling on
RewriteEngine On
RewriteBase /
###### Check for alias module to be installed ######
###### Domain without www ######
RewriteCond %{HTTP_HOST} ^www\.mysite\.com$ [NC]
RewriteRule ^(.*)$ http://mysite.com/$1 [R=301,L]
###### End www rewrite section ######
# Remove multiple slashes anywhere in URL
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
# Remove multiple slashes after domain
RewriteCond %{HTTP_HOST} !=""
RewriteCond %{THE_REQUEST} ^[A-Z]+\s//+(.*)\sHTTP/[0-9.]+$
RewriteRule .* http://%{HTTP_HOST}/%1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?$1 [L]
# show mysite.com/index.php always as mysite.com/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://mysite.com/ [R=301,L]
This will change www.mysite.com/index.php/////index.php
to mysite.com
which is three rules each with the [L]
switch in them
Upvotes: 0
Views: 162
Reputation: 9015
The Apache .htaccess file uses standard Apache configurations, such as Mod Rewrite. The Mod Rewrite docs explain how RewriteCond and RewriteRule operate.
I will break down your ModRewrite question:
RewriteCond %{HTTP_HOST} ^www\.mysite\.com$ [NC]
%{HTTP_HOST}
means the RewriteCond is checking the host name of the request^www\.mysite\.com$
is the pattern, checks if the host name matches www.mysite.com [NC]
mean no-case, makes the pattern matching case-insensitiveFor example, if someone requests http://www.mysite.com/index.html, the RewriteCond
would check the host www.mysite.com against the pattern ^www\.mysite\.com$
, and since it matches, the following RewriteRule would execute.
RewriteRule ^(.*)$ http://mysite.com/$1 [R=301,L]
^(.*)$
is the pattern, which will match everything. If the pattern does not match, the RewriteRule will not execute.http://mysite.com/$1
is the rewrite, basically change the request from www.
mysite.com to mysite.com.[R=301,L]
means the rewrite should send a 301 redirect and be the last rewrite rule to run. Continuing the example of http://www.mysite.com/index.html, which passes the RewriteCond, /index.html would match the ^(.*)$
pattern, the rewrite would be set to http://mysite.com/index.html and be sent as a 301 redirect and no further RewriteRules
would happen.
EDIT: Pondering the config
From my experience, you want all of your redirects at the top of your config. Especially in your cause, were you are trying to normalize your urls, the request will be washed through the series of redirects. Once that is finished, you then do rewrites to serve the correct content. With that in mind, here is a tweaked config of the last 2 rules:
# show mysite.com/index.php always as mysite.com/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://mysite.com/ [R=301,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/(.*)$ /index.php?a_param=$1 [QSA]
Off the top of my head I cannot validate the RewriteCond %{THE_REQUEST}
, but it looks good. I suspect you want the [QSA] flag which is the query string append. This would cause http://mysite.com/index.php?user=blah to redirect to http://mysite.com/?user=blah. You will want to add QSA to the other redirects if you need to pass along the query strings with the redirect.
The last rule is determines if index.php should be used versus static content in the directory of the htaccess (directory, images, js, etc). You do not want that as a redirect, but as a rewrite. Do not use a [R] or [L], but add [QSA] again. The pattern matched should be set to a specific parameters passed to index.php.
This combined with the previous rule would cause http://mysite.com/index.php?user=blah to redirect to to http://mysite.com/?user=blah and then be rewritten so index.php is used for http://mysite.com/?a_page=&user=blah. The request http://mysite.com/a_page?id=7 would be rewritten so index.php is used for http://mysite.com/?a_param=a_page&user=blah
Upvotes: 1
Reputation: 54757
Check out the Apache documentation. It explains what all the options in []
brackets mean.
[NC]
is no-case (or case-insensitive)[R=301]
is a 301 (permanent) redirect[L]
means last rule or stop rewriting%{HTTP_HOST}
is a server or environment variable representing the base URL of the host, such as www.example.com
.
If you understand regular expressions, you should also understand that the grouping (.*)
matches all characters between the start ^
and end $
of the path (the entire path), which you can then use in the second part as $1
.
Upvotes: 1