Reputation: 4898
Could someone explain how the mod_rewrite script below works? The goal is to catch an access to probe.com that has nothing following it and redirect that to probewebsites.com. If the access is to probe.com/anything, such as probe.com/register.php, the access stays at probe.com, accessing, in this case, probe.com/register.php.
The script works, I'm just not sure how.
I see that line 2 looks for an access to probe.com. Then line 3 somehow verifies that nothing is following probe.com. Line 4 looks like it's collecting a backreference, but it's never used. Thanks for any help.
1. RewriteEngine on
2. RewriteCond %{HTTP_HOST} probe\.com [NC]
3. RewriteCond %{REQUEST_URI} ^/$
4. Rewriterule ^(.*)$ http://probewebsites.com/ [L,R=301]
Upvotes: 1
Views: 58
Reputation: 51711
Your general understanding about the rules is correct. Let me expand on that.
RewriteEngine on
Turns the mod-rewrite rules processing on for this directory.
RewriteCond %{HTTP_HOST} probe.com [NC]
Looks like it's looking for probe.com
but it's not. Since it's a regex pattern, .
would match any character. So, probeXcom
would also pass; and since no regex boundaries (^
, $
) have been specified, it becomes a sub-string match. That means csi.probe.com
or probe.com.uk
would also match. The correct pattern to match only probe.com
or www.probe.com
would be
RewriteCond %{HTTP_HOST} ^(www\.)?probe\.com$ [NC]
^
and $
match on the start and end boundaries of the %{HTTP_HOST}
which refers to the HTTP header by the same name. \.
escapes the regex's match-on-any-character .
and starts matching on the literal dot .
character now. The question mark in (www\.)?
makes the captured www.
optional. Finally, [NC]
stands for no case and makes the test case-insensitive.
RewriteCond %{REQUEST_URI} ^/$
This condition is actually not required because you can make the same match in the RewriteRule
itself. So, the new rule should look like
RewriteRule ^$ http://probewebsites.com/ [R=301,L]
Notice, it's not ^/$
and it's not a typo. The RewriteRule
pattern matches the directory path but doesn't include the leading /
as in %{REQUEST_URI}
.
[L]
marks the rule as last since we're redirecting anyway which is specified by [R=301]
. The status code 301
indicates to the browser that this redirect is permanent and so the browser will cache it. If you specify just [R]
, the server would do a 302
temporarily moved redirect.
So, your final .htacces should look like
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?probe\.com$ [NC]
RewriteRule ^$ http://probewebsites.com/ [R=301,L]
Upvotes: 2