.htaccess - infinite loop - .htaccess

I'm getting an infinite loop on this site - http://www.salesmelbourne.com
below is the .htaccess and I know the problem is in the last 4 lines - well I think so - can some offer some advice... thx
php_value session.gc_maxlifetime 259200
php_flag register_globals off
php_flag zlib.output_compression on
php_flag output_compression_level 6
<Files *>
Header set Cache-Control: "private, pre-check=0, post-check=0, max-age=0"
Header set Expires: 0
Header set Pragma: no-cache
</Files>
# File Upload 25MB
php_value post_max_size 20M
php_value upload_max_filesize 20M
php_value max_execution_time 1000
# use utf-8 encoding for anything served text/plain or text/html
AddDefaultCharset utf-8
# force utf-8 for a number of file formats
AddCharset utf-8 .html .css .js .xml .json .rss .php
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L]
# Internally redirect all pages to index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/ajax/pages/(.*)$ /www/ajax/pages/404_error.php [R=301]
RewriteRule . index.php [L]

# Internally redirect all pages to index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/ajax/pages/(.*)$ /www/ajax/pages/404_error.php [R=301]
RewriteRule . index.php [L]
What line #2 (from the end) does? It is not suppose to work anyway, since URL pattern in RewriteRule starts with NO leading slash, but you have it there (^/ajax/pages/(.*)$).
Since I do not know how that particular rule is expected to be working, there are 2 possible solutions (both will work -- it's all about the aforementioned line -- does it need those 2 conditions to function properly or not):
# I have no idea how this rule is supposed to work, but assume it's the way to go
RewriteRule ^ajax/pages/(.*)$ /www/ajax/pages/404_error.php [R=301]
# Internally redirect all pages to index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
or like this (if those conditions are important for that rule)
# I have no idea how this rule is supposed to work, but assume it's the way to go
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ajax/pages/(.*)$ /www/ajax/pages/404_error.php [R=301]
# Internally redirect all pages to index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]

Related

mod_rewrite rule is not working but I have the correct syntax

So my mod_rewrite rule isn't working to specify a specific php file for a URL.
Options -Indexes
php_value short_open_tag 1
#Caching
# 1 HOUR
<FilesMatch "\.(ico|pdf|flv)$">
Header set Cache-Control "max-age=31536000, public"
</FilesMatch>
# 1 HOUR
<FilesMatch "\.(jpg|jpeg|png|gif|swf)$">
Header set Cache-Control "max-age=31536000, public"
</FilesMatch>
# 1 HOUR
<FilesMatch "\.(xml|txt|css|js)$">
Header set Cache-Control "max-age=31536000, proxy-revalidate"
</FilesMatch>
<IfModule mod_rewrite.c>
#Rewrite rules
RewriteEngine on
RewriteRule ^about\/special-offers\/?$ about/special-offers.php
# remove www. from all domains
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [L,R=301]
# Add trailing slash
# TODO: This breaks POST data when forwarding to a URL without an ending slash
#RewriteCond %{REQUEST_URI} !\.(php|html?|gif|jpe?g|JPE?G|png|bmp|js|swf|css|ico|xml|txt|woff2|woff)$
#RewriteRule ^(.*)([^/])$ http://%{HTTP_HOST}/$1$2/ [P,R=301]
# hide .svn folders
RewriteRule ^(.*/)*\.svn/ / [NC,L]
# send all images and misc files to the actual location
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteRule \.(gif|jpe?g|JPE?G|png|bmp|js|swf|ico|xml|txt) index.php [NC,L]
# send folder to the relevant php file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteCond %{REQUEST_FILENAME} !-f
#RewriteRule ^([^/]+)/([^/]+)/([^/]+)/$ $1/$2.php
RewriteRule ^([^/]+)\/([^/]+)\/ $1/$2.php
#RewriteRule ^([^/]+)/([^/]+)/$ $1/$2.php
RewriteRule ^([^/]+)/([^/]+)/$ $1/default.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
# otherwise if the file or directory is not found, process through the index
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
</IfModule>
I have a multitude of other rules in here, I can post them if need be, but since this is a specific rule I don't know why one of them would be overriding it. Direction is appreciated.
I've tried putting it at the top or bottom of my mod_rewrite section but neither seems to have any affect.
Something I've just noticed is that when I go to /about/special-offers/x (where x is any string), it loads the correct page.
Other
Finally fixed this issue, I did it by marking the RewriteRule as final with [L].
Relevant portion:
# send folder to the relevant php file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)\/([^/]+)\/ $1/$2.php [L]

Why is a / at the end of my URLs giving me a 500 error and why is .htaccess not working

I am running a simple HTML site, with a .htaccess edit. I have several issues.
If I don't put https then - www.example.com/apply will redirect to https www.xyz.com/apply.html - I understand I have the https redirect on. However, I also have it so .html is removed.
https www.xyz.com/apply will load fine
https www.xyz.com/apply/ will give a 500 error because there is a / at the end
.htaccess code is as follows
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ $1.html [L,QSA]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(([A-Za-z0-9\-]+/)*[A-Za-z0-9\-]+)?$ $1.php
<IfModule mod_headers.c>
<FilesMatch ".(js|css|xml|gz|html)$">
Header append Vary: Accept-Encoding
</FilesMatch>
</IfModule>
<filesMatch ".(css|jpg|jpeg|png|gif|js|ico)$">
Header set Cache-Control "max-age=2592000, public"
</filesMatch>
Any ideas? I wrote the code from memory so hopefully I'm not missing something.
Your problem is that you consider all wrong requests without / then you handle them like that , so you should check first whether a wrong request has / or not , then remove it internally , if you want a file look like directory,then handle it by the next rules , priority will be for html then to php so , your rules should look like this:
Options +FollowSymLinks -MultiViews
RewriteEngine On
#as per your comment that all css files under /css directory i added the
#following rules
RewriteCond %{REQUEST_URI} !^/css
RewriteCond %{REQUEST_URI} \.css$
RewriteRule ^(.+)/css/(.*)$ /css/$2 [L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.+)/$
RewriteRule ^ %1 [L,QSA]
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.+)$ $1.html [L,QSA]
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+)$ $1.php [L,QSA]
NOTE: Clear browser cache the test .
Update:
If you want CSS files work while you adding / to files , there is a workaround , for example you put all CSS files in cssfolder directory , so when the request come to either html or php files without trailing slash , css will load normally like /cssfolder/file.css , but with trailing slash the path will be different /file/cssfolder/file.css , you could add rule to remove any request end with css that has any thing before cssfolder directory .
Like that :
RewriteCond %{REQUEST_URI} !^/cssfolder
RewriteCond %{REQUEST_URI} \.css$
RewriteRule ^(.+)/cssfolder/(.*)$ /cssfolder/$2 [L]
These rules above should be after RewriteEngine On at you rules above.

.htaccess file makes page load indefinitely?

I'm using the following .htaccess so that pages preceded by /cn/ load with http (the rest loads with https:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# http requests anything other than /cn should become https
RewriteCond %{HTTPS} off
RewriteRule !^cn(/.*)?$ https://%{HTTP_HOST}%{REQUEST_URI} [R=302,NC,L,NE]
# https requests for /cn should become http
RewriteCond %{HTTPS} on
RewriteRule ^cn(/.*)?$ http://%{HTTP_HOST}%{REQUEST_URI} [R=302,NC,L,NE]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>
php_value upload_max_filesize 110M
php_value post_max_size 110M
The strange thing is that the page just keeps loading forever. What could be causing this?

Rewriting urls with Htaccess like filename/param1/param2 etc etc

I'm looking for a solution for the following situation. I'd like to covert the following url
www.example.com/filename.php?p1=1&p2=2 etc
to
www.exmaple.com/filename/1/2 etc
I managed to do this, with index.php, but I'd like to use other file aswell, not only the index. Is it possible? Here's my current code.
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
RewriteRule ^(filename)/([^/]+)/([^/]+)$ /$1.php?p1=$2&p2=$3 [L,QSA,NC]
ErrorDocument 404 /404.php
# display no errs to user
php_flag display_startup_errors off
php_flag display_errors off
php_flag html_errors off
This works for the php removal. But if I add parameters after it, it gives me error 500
Insert this rule just below RewriteEngine On line:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(filename)/([^/]+)/([^/]+)/?$ /$1.php?p1=$2&p2=$3 [L,QSA,NC]

htaccess rule return funny results in $_GET variable

this is a snippet of my htaccess, at the bottom is the redirect for user accounts.
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
RewriteRule ^(.*)$ /$1/ [R=301,L]
</IfModule>
Options -MultiViews
Options +FollowSymlinks
Options -Indexes
ErrorDocument 404 /core/error/templates/404/
AddDefaultCharset utf-8
AddCharset utf-8 .html .css .js .xml .json .rss .php
<IfModule mod_rewrite.c>
RewriteRule "(^|/)\." - [F]
</IfModule>
RewriteRule ^favicon.ico favicon.ico [NC,L]
# ----------------------------------------------------------------------
# RewriteRules
# ----------------------------------------------------------------------
# User profile redirect
#RewriteRule ^([^/]+)?$ /user/$1 [NC,R]
RewriteRule ^/([^/]+)/?$ profile.php?user=$1 [NC,L]
if i go to www.foo.bar/samsam it should populate the $_GET['user'] with "samsam" but when i do an echo it actually returns $_GET['user'] = profile.php
any ideas as to why its behaving this way?
You're close, but you need a couple of fixes to that last line. You'll need to remove the leading "/" character in your regexp. You'll also need to filter your last RewriteRule so it only affects non-existent files and directories. So... replace it with the following:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ profile.php?user=$1 [NC,L]

Resources