# THE LINE I'M ADDING
# RewriteCond %{REQUEST_URI} !^/InvalidLink\.html$
There is nothing "wrong" with the condition you are adding, providing the requested URL is exactly /InvalidLink.html
(case-sensitive match), this .htaccess
file is in the document root and the browser cache has been cleared before testing. So, this is still a puzzle. Try adding the NC
flag - in case there is a mismatch in case, for example:
RewriteCond %{REQUEST_URI} !^/InvalidLink\.html$ [NC]
The fact that the exception "works" if you first create this file, in which case the first condition (!-f
) prevents the rewrite happening, suggests that the file is being processed OK and your cache is clear. And that the above condition is at fault. However, from what you have posted it looks OK!?
An alternative to adding a condition to your existing rule, is to create a separate "exception" at the top of your file. So, instead of saying, "rewrite the URL if the request is not for /InvalidLink.html
", we can add a separate rule that states "if the URL is /InvalidLink.html
then stop here and do nothing more".
So, instead of your condition, include a separate rule at the top of your file:
RewriteRule ^InvalidLink\.html$ - [NC,L]
Note there is no slash prefix on the RewriteRule
pattern in per-directory .htaccess
files. This will match /InvalidLink.html
and prevent the following mod_rewrite directives from being processed. Ideally, the NC
flag should not be necessary, however, I've included it "just in case".
This is really subsidiary to your main problem above...
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
This carbuncle is what cPanel automatically adds before every RewriteRule
when implementing the "Let's Encrypt" SSL cert. It's very messy and there is much argument in the cPanel forums requesting this be removed or changed. (Why they can't add a single code block/exception at the top of the file is a mystery.)
Anyway, in the interim, these directives can be safely removed (throughout the file) - they are not required (after the cert has been installed). This will simplify your directives, making the file more readable (and avoid any potential conflict). However, cPanel will later automatically re-add these directives when the certs are renewed (every 3 months).
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^(.*)$ "http\:\/\/example\.com\/$1" [R=301,L]
As mentioned above, the middle conditions can be safely removed. However, this canonical redirect is in the wrong place! It needs to be at the very top of your file, otherwise none of your shortened URLs will be canonicalised. In short, cPanel is not very good (unreliable) at editing the .htaccess
file!
Summary
Bringing all the above together, your existing .htaccess
file can be rewritten as:
RewriteEngine On
# Canonical www to non-www redirect
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule (.*) http://%1/$1 [R=301,L]
# Exceptions - avoid these URLs being rewritten
RewriteRule ^InvalidLink\.html$ - [NC,L]
# BEGIN YOURLS
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /yourls-loader.php [L]
# END YOURLS
The <IfModule>
wrapper is not required. And the RewriteBase
directive was superfluous. .*
is the same as ^.*$
. Backslash escaping chars in the RewriteRule
substitution is unnecessary (that's "typical cPanel").
/InvalidLink.html
exist as a physical file? – MrWhite Feb 9 '17 at 8:48InvalidLinks.html
actually existed as an actual file (since it just looks like a real file) is that the existing directives would already exclude it from being rewritten. – MrWhite Feb 9 '17 at 12:50