Mobile app version of vmapp.org
Login or Join
Cooney921

: RewriteCond to not rewrite a specific URI not working I am working with a URL shortener (YOURLS) and I'm trying to have a certain page not directed to the link processing script. Here's the

@Cooney921

Posted in: #Apache #Htaccess #ModRewrite

I am working with a URL shortener (YOURLS) and I'm trying to have a certain page not directed to the link processing script. Here's the original .htaccess:

# BEGIN YOURLS
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/[0-9]+..+.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}.txt(?: Comodo DCV)?$
# THE LINE I'M ADDING
# RewriteCond %{REQUEST_URI} !^/InvalidLink.html$
RewriteRule ^.*$ /yourls-loader.php [L]
</IfModule>
# END YOURLS
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/" [R=301,L]


I've added a new condition right before the RewriteRule line (commented out and pointed out above):

RewriteCond %{REQUEST_URI} !^/InvalidLink.html$


to have example.com/InvalidLink.html ignored. But this is making no difference. What am I doing wrong here?

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Cooney921

1 Comments

Sorted by latest first Latest Oldest Best

 

@Ann8826881

# 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/" [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 (.*) %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").

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme