Apache Mod_Rewrite condition (skip directory) not working? - apache

So I have:
-A mod_rewrite rule for a few things, including remove www., force https, and SEO
-A directory called stage that I want these rules to not apply to
Obviously as I'm posting here, the condition isn't being applied or met, and I have no idea why :(
A warning, I manually and automatically (using cPanel) manage my .htaccess, so it's pretty messy and I apologize sincerely in advance. I'd be very grateful if anyone could point out redundancy (I think I may see one but I'm unsure).
I appreciate any help anyone can offer me, even tips and not answers! I'm a beginner (relatively) and a software engineering student, so anything helps :) It may also explain my newbiness.
Here's my .htaccess file for the root public_html directory:
Thanks in advance :)
suPHP_ConfigPath /home/patters1/public_html
#AddType text/css .css
Options -Indexes
ExpiresActive On
ExpiresDefault "access plus 1 month"
# WEEK
<FilesMatch "\.(js|css|swf)$">
Header set Cache-Control "public, max-age=604800"
</FilesMatch>
RewriteEngine On
RewriteCond %{REQUEST_URI} !/selfcontrol
RewriteCond %{REQUEST_URI} !/myadmin
RewriteCond %{REQUEST_URI} !/stage
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9]+)$ index.php?a=$1
RewriteCond %{REQUEST_URI} !/selfcontrol
RewriteCond %{REQUEST_URI} !/stage
RewriteCond %{REQUEST_URI} !/myadmin
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9]+)/$ index.php?a=$1
RewriteCond %{HTTP_HOST} ^www\.pattersoncode\.ca$
RewriteRule ^/?$ "https\:\/\/pattersoncode\.ca\/" [R=301,L]
RewriteCond %{HTTP_HOST} ^!.pattersoncode\.ca$
RewriteRule ^/?$ "https\:\/\/pattersoncode\.ca\/" [R=301,L]
RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteCond %{REQUEST_FILENAME}.gz -f
RewriteRule ^(.*)$ $1.gz [L]
RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
RewriteRule .* – [F,L]
RewriteCond %{HTTP_HOST} !^pattersoncode\.ca$
RewriteRule ^/?$ "https\:\/\/pattersoncode\.ca\/" [R=301,L]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://pattersoncode.ca/$1 [R=301,L]
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript
ErrorDocument 404 /resources/error.php?a=das
ErrorDocument 401 /resources/error.php?a=4sdfdsc
ErrorDocument 403 /resources/error.php?a=4fsd
ErrorDocument 500 /resources/error.php?a=500fdf
ErrorDocument 400 /resources/error.php?a=400dsf

I would recommend to check your .htaccess file with this tool by commenting all your lines and uncommenting it one rule at a time. There are many errors / typo / duplicate lines in your code.
Here are a few errors:
Here is a duplicated rule:
RewriteCond %{REQUEST_URI} !/selfcontrol
RewriteCond %{REQUEST_URI} !/myadmin
RewriteCond %{REQUEST_URI} !/stage
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9]+)$ index.php?a=$1
RewriteCond %{REQUEST_URI} !/selfcontrol
RewriteCond %{REQUEST_URI} !/stage
RewriteCond %{REQUEST_URI} !/myadmin
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9]+)$ index.php?a=$1
Here you wrote this but I think that you meant !^pattersoncode\.ca$:
RewriteCond %{HTTP_HOST} ^!.pattersoncode\.ca$
And you could have combine it with the previous condition
RewriteCond %{HTTP_HOST} ^www\.pattersoncode\.ca$ [OR]
RewriteCond %{HTTP_HOST} ^!.pattersoncode\.ca$
Why this rule should only work on port 80 contrary to the previous ones ?
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://pattersoncode.ca/$1 [R=301,L]
You should remove the " characters from this rule
RewriteRule ^/?$ "https\:\/\/pattersoncode\.ca\/" [R=301,L]

Related

Laravel redirect www to non-www with htaccess not working correctly

I have the following lines inside an .htaccess file which redirects a www URL to a non-www URL:
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
It works correctly when trying to load the index route: https://www.example.com to https://example.com, but if someone tries to load any other route, it does not load anything, for example, if I try to load https://www.example.com/about-us it converts the URL to https://example.com/index.php and it doesn't load anything.
How can I fix this?
EDIT
My complete .htaccess is the following:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteCond %{REQUEST_URI} !(\.css|\.pdf|\.mp4|\.woff|\.woff2|\.ttf|\.js|\.png|\.jpg|\.jpeg|\.gif|robots\.txt)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(css|js|images|favicon|fonts|font|videos|storage|pdf)/(.*)$ public/$1/$2 [L,NC]
</IfModule>
<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin "*"
</IfModule>
<FilesMatch ".(eot|ttf|otf|woff|woff2)">
Header set Access-Control-Allow-Origin "*"
</FilesMatch>
<Files .env>
order allow,deny
Deny from all
</Files>
you can try this
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [L,NE,R=301]

.htaccess: Remove extension and force traling slash (seo friendly)

I've been playing about with my .htaccess file and so far that's doesn't work.
I'm trying to force the .www prefix, while removing the .html extension and adding/force a trailing slash at the end of URL.
Example (with test.html file):
http://www.domain.com/test.html
http://www.domain.com/test
http://domain.com/test.html
http://domain.com/test
= http://www.domain.com/test/
My .htaccess:
Options +FollowSymLinks +MultiViews
RewriteEngine on
Rewritecond %{HTTP_HOST} ^laforgenumerique.fr$
Rewriterule ^(.*) http://www.laforgenumerique.fr/$1 [QSA,L,R=301]
#REMOVE DOT HTML
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.html [L]
#FORCE TRAILING SLASH
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^[^/]+$ %{REQUEST_URI}/ [L,R=301]
#CUSTOM ERROR DOCUMENT
ErrorDocument 400 http://www.laforgenumerique.fr/404.html
ErrorDocument 401 http://www.laforgenumerique.fr/404.html
ErrorDocument 403 http://www.laforgenumerique.fr/404.html
ErrorDocument 404 http://www.laforgenumerique.fr/404.html
ErrorDocument 500 http://www.laforgenumerique.fr/404.html
Please help!
Host: OVH
You wrote:
#REMOVE DOT HTML
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.html [L]
#FORCE TRAILING SLASH
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^[^/]+$ %{REQUEST_URI}/ [L,R=301]
It seems that you are trying to mix two separate rules.
[L] suffix means that this is the last rule; all rules below will be skipped even if they are suitable.
You may try something like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ www.$1
RewriteRule ^(.*).html$ $1/ [L]

Problems with Apache htaccess redirect

I have this htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^/4b/ / [R=301,L]
# This portion checks for the presence of maintenance.enable to toggle
# maintenance mode
RewriteCond %{DOCUMENT_ROOT}/maintenance.html -f
RewriteCond %{DOCUMENT_ROOT}/maintenance.enable -f
RewriteCond %{SCRIPT_FILENAME} !maintenance.html
RewriteRule ^.*$ /maintenance.html [R=503,L]
ErrorDocument 503 /maintenance.html
Header Set Cache-Control "max-age=0, no-store"
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ /index.php?/$1 [L]
</IfModule>
But it doesn't match the URL http://test.com/4b/ or http://test.com/4b -- instead, it gives me a 404. What am I doing wrong?
Apparently, I added an extra / in ^/4b/ when it should have been just ^4b/

Apache htaccess - route all requests to index.php except those to certain directories

Currently, my .htaccess file is routing all requests where the file does not exist to index.php. I would like to alter this so that it routes all requests to index.php regardless of whether or not the file exists, except for certain directories/paths. (such as /js, /css, /img)
Here is my current configuration:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
I'm having trouble figuring out how to do this with the Google or apache's documentation. Help is much appreciated, as are any general tips on this issue.
The following should work
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} ! ^/css/ [OR]
RewriteCond %{REQUEST_FILENAME} ! ^/img/ [OR]
RewriteCond %{REQUEST_FILENAME} ! ^/js/
RewriteRule . /index.php [L]
</IfModule>

.htaccess subdirectory redirect with non www to www and parameters

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /qsg/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?Item=$1 [L]
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /qsg/
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?Item=$1 [L]
</IfModule>
I tried to look for an answer, but haven't been able to find one.
I am trying to redirect:
http://example.com/qsg/abcd
to
http://www.example.com/qsg/index.php?Item=abcd
I can get this working fine when the www is present in the URI. When I remove the www, it redirects to http://www.example.com/404.shtml
I've tried the two methods above with both the same result. I just can't figure out what I am doing wrong. I've seen plenty of other examples where this should work, but not for me. Do I have something wrong in my .htaccess file is there a possibility of something else causing the bad redirect?
Note: both the above rewrites are not included in the file. I've tried both independently.
Give this one a shot:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /qsg/
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?Item=$1 [L,PT]
</IfModule>