Forum Moderators: phranque
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^business-mobile.htm /blahtest.htm [L,R=301]
RewriteRule ^(.*/)?\.svn/ - [F,L]
ErrorDocument 403 "Access Forbidden"
RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
RewriteRule .* – [F,L]
</IfModule> <IfModule mod_rewrite.c>
RewriteEngine On
RedirectPermanent business-mobile.htm blahtest.htm
RewriteRule ^(.*/)?\.svn/ - [F,L]
ErrorDocument 403 "Access Forbidden"
RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
RewriteRule .* – [F,L]
</IfModule> <IfModule mod_rewrite.c>
RewriteEngine On
RedirectPermanent business-mobile.htm blahtest.htm
RewriteRule ^(.*/)?\.svn/ - [F,L]
ErrorDocument 403 "Access Forbidden"
RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
RewriteRule .* – [F,L]
</IfModule> RewriteRule ^(.*/)?\.svn/ - [F,L] ErrorDocument 403 "Access Forbidden" should come out as we do not use svn any longer (this is quite an old file / website) but other than that, I am quite far out of my depth here. I'd really appreciate any advice from anyone familiar with how an htaccess file functions?
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^business-mobile.htm /blahtest.htm [L,R=301]
RewriteRule ^(.*/)?\.svn/ - [F,L]
ErrorDocument 403 "Access Forbidden"
RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
RewriteRule .* – [F,L]
</IfModule>
RewriteRule ^business-mobile.htm$ https://www.example.com/blahtest.htm [L,R=301] - the <IfModule mod_rewrite.c> envelope is unnecessary.
you already know mod_rewrite is installed, so no need to check for it.
- if you are using mod_rewrite anywhere, you must use mod_rewrite everywhere.
that means forgoing any redirects with mod_alias directives (i.e. RedirectPermanent) and using an equivalent RewriteRule directive, as in this version.
- the [F] flag send a 403 response, so the last 4 directives specify the custom 403 error message and send 403 responses to any requests for paths ending in ".svn/" or any requests by user agents identifying as libwww-perl.
- if a ".svn/" path no linger exists you can remove that rule and the response will be a 404 instead of a 403.
- if your RewriteRule is a 301 redirect you usually want to specify the full canonical protocol and hostname in the substitution string:
RewriteRule ^business-mobile.htm$ https://www.example.com/blahtest.htm [L,R=301]
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^business-mobile.htm$ https://www.example.com/blahtest.htm [L,R=301]
RewriteRule ^(.*/)?\.svn/ - [F,L]
ErrorDocument 403 "Access Forbidden"
RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
RewriteRule .* – [F,L]
</IfModule>
what response did you get for the RewriteRule version when https://www.example.com/business-mobile.htm is requested?
[edited by: phranque at 11:57 am (utc) on Jan 4, 2019]
[edit reason] Please Use example.com [webmasterworld.com] [/edit]
Is there a way for me to check that this 'mod_rewrite' is definitely installed? Unfortunately i don't know that this is the case.
apachctl -M i've tried to create a file called 'info.php' in the root directory of the website, as this is apparently a way to show which modules are loaded or installed, but this doesnt seem to work?
RewriteRule foobar http://example.com/widget.html [R=302,L]
replacing "http://example.com" with your own protocol and sitename. Now open your browser and request example.com/foobar (or anything in your site, so long as the URL includes the string "foobar"). Did you get redirected to widget.html? You will, of course, see your site's ordinary 404 page, but the browser address bar will say widget.html I am trying to add a straighforward redirect to the .htaccess file of the website
Redirect 301 /business-mobile.htm https://www.example.com/blahtest.htm ErrorDocument 404 /errors/404.htm
# Redirect old page
Redirect 301 /business-mobile.htm https://www.example.com/blahtest.htm
# Custom errors
ErrorDocument 404 /errors/404.htm
# Invoke mod_rewrite
RewriteEngine On
# Identify undesirable UA
RewriteCond %{HTTP_USER_AGENT} libwww-perl [NC]
# Block access
RewriteRule /*$ – [F]
Do not mix mod_alias redirects with mod_rewrite redirects.
When not to use mod_rewrite
mod_rewrite should be considered a last resort, when other alternatives are found wanting. Using it when there are simpler alternatives leads to configurations which are confusing, fragile, and hard to maintain. Understanding what other alternatives are available is a very important step towards mod_rewrite mastery.
Simple Redirection
mod_alias provides the Redirect and RedirectMatch directives, which provide a means to redirect one URL to another. This kind of simple redirection of one URL, or a class of URLs, to somewhere else, should be accomplished using these directives rather than RewriteRule.
when there are Redirect and RewriteRule directives in the same scope, the RewriteRule directives will run first, regardless of the order of appearance in the configuration file
The most common situation in which mod_rewrite is the right tool is when the very best solution requires access to the server configuration files, and you don't have that access. Some configuration directives are only available in the server configuration file. So if you are in a hosting situation where you only have .htaccess files to work with, you may need to resort to mod_rewrite.Et cetera et cetera. Source: the same page as above.
...
The use of RewriteRule to perform this task may be appropriate if there are other RewriteRule directives in the same scope. This is because, when there are Redirect and RewriteRule directives in the same scope, the RewriteRule directives will run first, regardless of the order of appearance in the configuration file.
In the case of the http-to-https redirection, the use of RewriteRule would be appropriate if you don't have access to the main server configuration file, and are obliged to perform this task in a .htaccess file instead.
I didn't mix them, they are entirely separate (the Rewrite directives will run first).
Word for the day: cherry-picking
Do not mix mod_alias redirects with mod_rewrite redirects.
assuming there is or eventually will be a hostname canonicalization redirect (which will necessarily use mod_rewrite), you are potentially creating an unnecessary chained redirect
assuming there is or eventually will be a hostname canonicalization redirect (which will necessarily use mod_rewrite), you are potentially creating an unnecessary chained redirect
... that is not true
Redirect 301 /test https://www.example.com/blahtest
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$ [NC,OR]
RewriteCond %{HTTPS} !=on
RewriteRule (.*) https://www.example.com/$1 [R=301,L] RewriteEngine On
RewriteRule ^test$ https://www.example.com/blahtest [R=301,L]
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$ [NC,OR]
RewriteCond %{HTTPS} !=on
RewriteRule (.*) https://www.example.com/$1 [R=301,L] The boilerplate is about halfway down the thread.
Step 4a: Get rid of mod_alias. If your htaccess file contains any mod_rewrite directives, it can't use mod_alias (Redirect... by that name), or things may happen in the wrong order.
Understanding what other alternatives are available is a very important step towards mod_rewrite mastery... mod_rewrite should be considered a last resort, when other alternatives are found wanting
q.e.d.
# Redirect old page
Redirect 301 /test.htm https://www.example.com/test/test.htm
# Custom errors
ErrorDocument 404 /errors/404.htm
# Invoke mod_rewrite
RewriteEngine On
# Canonical & Encryption
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC,OR]
RewriteCond %{HTTPS} !=on
RewriteRule (.*) https://www.example.com/$1 [R=301,L]
# Identify undesirable UAs
RewriteCond %{HTTP_USER_AGENT} libwww-perl [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^-$
# Block access
RewriteRule /*$ - [F]
example.com/test.htm
[05/Jan/2019:16:24:48 +0000] "GET /test.htm HTTP/1.1" 301 617 "-"
[05/Jan/2019:16:24:49 +0000] "GET /test/test.htm HTTP/1.1" 200 38 "-"
I am trying to add a straightforward redirect to the .htaccess file of the website i'm (supposedly) administering.
it must be magic.It's a kind of magic that doesn't happen on my own site, if so. In an excess of paranoia--even though I know I have tested this before, since that's kinda the whole point of having a test site*--I tried it again, using my tried-and-true foobar-to-widget pattern. (The 302 is because** the RedirectMatch rule was put in for testing and therefore didn't say anything about Permanent or 301.)
Why did the second redirect (mod_alias redirect to https://www.example.com/widget.html) take up fully 165 more bytes than the first redirect (mod_rewrite redirect to https://www.example.com/foobar)? Aw, heck, who knows.
it must be magic
Apache docs are intended primarily for server administrators
Understanding what other alternatives are available is a very important step towards mod_rewrite mastery.
RewriteRule foobar http://example.com/widget.html [R=302,L] to the existing htaccess file here; unfortunately, this does not seem to work on the website that is currently live. I do not see the text that you've outlined in the URL.
# Redirect old page
Redirect 301 /business-mobile.htm https://www.myurl.com/blahtest.htm
# Custom errors
ErrorDocument 404 /404.htm
# Invoke mod_rewrite
RewriteEngine On
# Canonical & Encryption
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.myurl\.com [NC,OR]
RewriteCond %{HTTPS} !=on
RewriteRule (.*) https://www.myurl.com/$1 [R=301,L]
# Identify undesirable UAs
RewriteCond %{HTTP_USER_AGENT} libwww-perl [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^-$
# Block access
RewriteRule /*$ - [F]
I do not see the text that you've outlined in the URL.Uh-oh, this calls for more investigating. If you've added the experimental line or, for that matter, any similar experimental line--replacing example.com with your real sitename, of course--and request foobar.html, where do you end up? Matter of fact, it doesn't actually matter if you forgot to make the change, because then you'd end up at the real example dot com seeing their 404 screen instead of yours.
Things definitely happen in the right order.If you define “right order” as “two separate redirects” then there is nothing more to be said.
there is nothing more to be said
Note that mod_rewrite tries to guess whether you have specified a file-system path or a URL-path by checking to see if the first segment of the path exists at the root of the file-system. For example, if you specify a Substitution string of /www/file.html, then this will be treated as a URL-path unless a directory named www exists at the root or your file-system (or, in the case of using rewrites in a .htaccess file, relative to your document root), in which case it will be treated as a file-system path. If you wish other URL-mapping directives (such as Alias) to be applied to the resulting URL-path, use the [PT] flag as described below.
passthrough|PT Forces the resulting URI to be passed back to the URL mapping engine for processing of other URI-to-filename translators, such as Alias or Redirect.
# Redirect old page
Redirect 301 /test.htm https://www.example.com/test/test.htm
# Invoke mod_rewrite
RewriteEngine On
# Canonical & Encryption
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC,OR]
RewriteCond %{HTTPS} !=on
RewriteRule (.*) https://www.example.com/$1 [R=301,PT]
Perhaps your problematic RewriteRules need a [PT] flag.This thread is about .htaccess. In fact, the word appears in the thread title. The [PT] flag is not used in htaccess, because, quote,
the PT flag is implied in per-directory contexts such as <Directory> sections or in .htaccess files
The [PT] flag is not used in htaccess
The only way to circumvent that is to rewrite to -.
The use of the [PT] flag causes it to be treated as a URI instead. That is to say, the use of the [PT] flag causes the result of the RewriteRule to be passed back through URL mapping, so that location-based mappings, such as Alias, Redirect, or ScriptAlias, for example, might have a chance to take effect.
Get rid of mod_alias. If your htaccess file contains any mod_rewrite directives, it can't use mod_alias (Redirect... by that name), or things may happen in the wrong order.
And the point you wilfully miss is that Apache 2.4 is built to use mod_rewrite and mod_alias in sequence by design.
The use of the [PT] flag ...
Nowadays, things definitely happen in the right order.
it must be magic
Avoid using mod_alias and mod_rewrite together unless you either have access to the Apache configuration above Directory level or are on a LiteSpeed server (where the two modules seem to play nice).
The current Apache .htaccess implementation omits the Passthrough option offered at VirtualHost level, so a rewritten URI cannot be passed internally to mod_alias before execution (as it sometimes needs to be) in a shared hosting environment.
This may lead to a chain of two 301 Permanent redirects being executed, generally undesirable and not popular with search engines.
A future Apache release needs to address this issue - mod_rewrite and mod_alias will play nice if you ask them, but you can't ask them from a shared hosting account.