Having some trouble with .htaccess and conflicting rules.
Code below:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Options +FollowSymLinks
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
DirectoryIndex index.php
RewriteRule ^home/?$ index.php [L]
RewriteRule ^([a-zA-Z0-9-]+)?$ static-page.php?slug=$1 [L]
The pages based on static-page.php work fine but when I try going to '/home' the page loads for a while then gives me nothing.
I believe this is because the second rule is overwriting the first one.
Anyone know how to solve this?
Thanks,
Try this corrected code:
DirectoryIndex index.php
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{SCRIPT_FILENAME} -f [OR]
RewriteCond %{SCRIPT_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^home/?$ index.php [L]
RewriteRule ^([a-zA-Z0-9-]+)?$ static-page.php?slug=$1 [L,QSA]
Related
i've removed .php extensions first on my website. then i've forwarded www to non-www version. but there is a problem with that.
my .htaccess file looks like this:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^188\.166\.104\.194
RewriteRule (.*) http://example.com/$1 [R=301,L]
www forwards to non-www with that .htaccess. that is cool. but the problem is with the other files.
i'm now using: http://example.com/contact instead of: http://example.com/contact.php
but when you try to open http://www.example.com/contact that .htaccess forwards me to http://example.com/contact.php/
how do i fix that?
have a nice day!
Change the order of rules and some refactoring:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.example\.com|188\.166\.104\.194)$ [NC]
RewriteRule (.*) http://example.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
RewriteRule ^([^/]+)/$ $1.php [L]
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php [L]
Make sure to test is after clearing your browser cache.
Something with my .htaccess file is creating a rule loop of some sort when a certain condition exists. Here is the code in question:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$ [NC]
RewriteRule ^(.+)/?$ /%1.php?o=$1 [L,NC,QSA]
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com$ [NC]
RewriteRule ^$ /%1.php [L,NC,QSA]
</IfModule>
Normally what this does is:
test.example.com -> example.com/test.php
test.example.com/test2 -> example.com/test.php?o=test2
Thats fine, except when the "test" subdomain part points to a nonexistent file, I get an internal redirect error, specifically this:
Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace., referer: [retracted]
Any help is appreciated.
Yes it will cause looping because RewriteCond %{REQUEST_FILENAME} !-f will still be true for /test.php when /test.php doesn't exist.
You can use this code to prevent looping:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
# don't redirect after one internal redirect
RewriteCond %{ENV:REDIRECT_STATUS} .+
RewriteRule ^ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$ [NC]
RewriteRule ^(.+?)/?$ /%1.php?o=$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$ [NC]
RewriteRule ^$ /%1.php [L]
</IfModule>
I have been trying to figure out this for a while but no success-
I have this site structure http://example.com/catalog/current/sub-folders/..
The result should hide the folder "current" so that the paths look like http://example.com/catalog/sub-folders/
This is what I have so far-
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+current/([^\s]+) [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (?!^current/)^(.*)$ /current/$1 [L,NC]
when I place this .htaccess to the root and go to http://example.com/catalog/sub-folders/, it try to look for /current/catalog/sub-folders/
Any help to approach this problem will be highly appreciated.
Keep your code like this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \s/+(catalog)/current/(\S*) [NC]
RewriteRule ^ %1/%2 [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(catalog)/((?!current/).*)$ $1/current/$2 [L,NC]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.+)$ $1.php [L]
Try making the last rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (?!^hsc-bulletin/)^(.*)$ /current/$1 [L,NC]
This instead:
RewriteCond %{REQUEST_URI} !^/current/
RewriteRule ^(?!hsc-bulletin/)(.*)$ /current/$1 [L,NC]
I have rules for forcing www and suppressing php extensions. How can I consolidate them into one rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L
You cannot combine them into 1 rules since one rule is external redirect and 2nd in doing rewrite internal only.
But you can correct the 2nd rule a bit:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
I am struggling since last couple of hours to get it worked but no luck.. if any help will really be great.
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteRule ^(.+)/$ /$1 [R=301,L]
RewriteRule ^profile/(.*)$ details.php?profile=$1 [L,NC,QSA]
RewriteRule ^industry/(.*)$ category.php?industry=$1 [L,NC,QSA]
RewriteRule ^page/(.+)$ index.php?page=$1 [L,NC,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [R=301,L]
very complex is it? but really not it is easy but when i am trying to add one more rule in above set of rules it is not working.
actual url - http://www.examples.com/req-info.php?tag=denis-reach-home&about=denis-reach&info=home
To URL - http://www.examples.com/tag/denis-reach-home/denis-reach/home
i tried varies things but no luck.. e.g -
RewriteRule ^tag/([^/]*)/([^/]*)$ /req-info.php?tag=$1&about=$2info=$3 [L]
any expert can share some tips.
thank you in advance.
You're very close. Have this code in your .htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteRule ^(.+)/$ /$1 [R=301,L]
RewriteRule ^profile/(.*)$ details.php?profile=$1 [L,NC,QSA]
RewriteRule ^industry/(.*)$ category.php?industry=$1 [L,NC,QSA]
RewriteRule ^page/(.+)$ index.php?page=$1 [L,NC,QSA]
# your new rule
RewriteRule ^tag/([^/]+)/([^/]+)/([^/]+)/?$ req-info.php?tag=$1&about=$2info=$3 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [R=301,L]