How to config htaccess to html can execute php - .htaccess

I want to configure the htaccess file to execute php file in html, but I get an error 500 Internal Server.
this here:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTPS} !=on [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
AddType application/x-httpd-php .html .htm
</IfModule>

Related

.htaccess redirect entire site from non-www/http to www/https

I know this has been asked a lot on here but I still can't find an answer that works for my website. These are the posts/answers I've tried:
.htaccess redirect from non-www http subfolder to www https subfolder
Redirect non-www(http) and www(http) to https://www
htaccess non-www to www while http to https and http://www to https://www
Redirect NON-www to www for https
Htaccess Redirect: WWW + HTTPS://WWW + HTTP:// ---> HTTPS://non-www
.htaccess non-www to www and http to https redirects
htaccess redirect https://www. (https://www.example.com) to non www (https://example.com)
htaccess redirect from non-www with https to www with https
and many more...
I am getting duplicate content errors showing up, here is an example
https://www.example.co.uk/coffee-machines/
http://example.co.uk/coffee-machines/
http://www.example.co.uk/coffee-machines/
http://example.co.uk/coffee-machines/
These URLs are showing up as duplicate content so I am trying to force a 301 redirect to
https://www.example.co.uk/coffee-machines/
and not just on that url but site wide. I have a "Canonical" tag set up that points to the correct version but I'd also like to 301 redirect all web pages/directories to the www/https version of the page.
This is my current contents of .htaccess
Options -Indexes
Options +FollowSymLinks
Options -MultiViews
AddDefaultCharset UTF-8
ErrorDocument 401 /401.php
ErrorDocument 404 /404.php
RewriteEngine on
RewriteBase /
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
and this works for redirecting example.co.uk to the www/https version but no sub-pages.
Could someone please point me in the right direction to either redirect all urls to the www/https version of suggest a solution to stop the duplicate content.
I've changed the .htaccess to the code below to reflect the post being flagged as duplicate. So the .htaccess now reflects the answer in this post (Redirect non-www and non-http to https) and also included the entire .htaccess file contents.
I am checking the headers by starting a session with putty and then using (curl --head http://example.co.uk) as suggested by #StephenOstermiller
Options -Indexes
Options +FollowSymLinks
Options -MultiViews
AddDefaultCharset UTF-8
ErrorDocument 401 /401.php
ErrorDocument 404 /404.php
RewriteEngine on
RewriteBase /
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ https://www.xxx.co.uk%{REQUEST_URI} [NC,L,R=301,NE]
RewriteRule ^q&a/$ forum.php
RewriteRule ^q&a/(.*).html$ forumDetail.php?question=$1&%{QUERY_STRING} [B,L]
#RewriteRule ^q&a/(.*)$ /forum/$1 [R=301,NC,L]
RewriteRule ^blog/$ blog.php
RewriteRule ^blog/(.*).html$ blogDetail.php?blog_name=$1&%{QUERY_STRING} [B,L]
RewriteRule ^about-us/$ about.php
RewriteRule ^privacy-policy/$ privacy.php
RewriteRule ^disclaimer/$ disclaimer.php
RewriteRule ^my-account/$ user_account.php
RewriteRule ^activate-my-account/$ user_activate.php
RewriteRule ^update-my-account/$ user_change.php
RewriteRule ^login/$ user_login.php
RewriteRule ^logout/$ user_logout.php
RewriteRule ^register/$ user_register.php
RewriteRule ^reset-my-password/$ user_reset.php
RewriteRule ^home-energy/$ home-energy.php
RewriteRule ^compare/(.*)$ /kitchen-scales/$1 [R=301,NC,L]
RewriteRule ^kitchen_scales/(.*)$ /kitchen-scales/$1 [R=301,NC,L]
RewriteRule ^coffee_machines/(.*)$ /coffee-machines/$1 [R=301,NC,L]
RewriteRule ^digital_radios/(.*)$ /digital-radios/$1 [R=301,NC,L]
RewriteRule ^mixers_blenders/(.*)$ /mixers-blenders/$1 [R=301,NC,L]
RewriteRule ^4g-broadband-all-you-need-to-know.html$ 4g-broadband-all-you-need-to-know.php
Redirect 301 /kitchen-scales/category/digital-scales/index.php https://www.xxx.co.uk/kitchen-scales/category/digital-scales/
Redirect 301 /kitchen-scales/salter-kitchen-scales.html https://www.xxx.co.uk/kitchen-scales/brand/salter/
Redirect 301 /kitchen-scales/brand/index.php https://www.xxx.co.uk/kitchen-scales/brand/
Redirect 301 /kitchen-scales/brand/salter/index.php https://www.xxx.co.uk/kitchen-scales/brand/salter/
Redirect 301 /coffee-machines/category/bean-to-cup-coffee-machines/index.php https://www.xxx.co.uk/coffee-machines/category/bean-to-cup-coffee-machines/
Redirect 301 /coffee-machines/bean-to-cup-coffee-machines.html https://www.xxx.co.uk/coffee-machines/category/bean-to-cup-coffee-machines/
Redirect 301 /coffee-machines/bean-to-cup-coffee-machines.php https://www.xxx.co.uk/coffee-machines/category/bean-to-cup-coffee-machines/
<IfModule mod_headers.c>
Header set Connection keep-alive
</IfModule>
<FilesMatch "\.(ico|pdf|jpg|jpeg|png|webp|gif|html|htm|xml|txt|xsl|css)$">
Header set Cache-Control "max-age=31536000"
</FilesMatch>
AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript text/javascript
<FilesMatch "config\.php|config\.advanced\.php">
Order allow,deny
Deny from all
</FilesMatch>
<Files ~ "^.*\.([Hh][Tt][Aa])">
Order allow,deny
Deny from all
Satisfy all
</Files>
AddType x-httpd-php73 .php
I'm also using LetsEncypt - not sure if that's relevant?
When the problem is per-directory .htaccess files, you can prevent the rules in those files from running after some rules by using the [END] flag. From the documentation:
Using the [END] flag terminates not only the current round of rewrite processing (like [L]) but also prevents any subsequent rewrite processing from occurring in per-directory (htaccess) context.
So you should be able to use [END] in your rules to make them take precedence over other rules in the subdirectories:
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,END]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,END]

What is the code to be added to Redirect from http to https in .htaccess file?

What is the code to be added to Redirect from http to https in .htaccess file?
here is the existing code in .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/NextNext/
RewriteRule ^(.*)$ /public/$1 [L]
</IfModule>
awaiting your help..
Try this rule after RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

How to fix the error Too Many Redirects error in HTTP subdomain

I transfered a site from a primary domain to a subdomain. But i got the error ERR_TOO_MANY_REDIRECTS .
I tried to clean .htaccess with no result
DirectoryIndex index.html index.php
AddType text/x-component .htc
RewriteEngine on
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ http://kosmimashop.digital-media.gr/$1 [R=301,L]
RewriteRule ^home/ index.php?page=home&%{QUERY_STRING}
RewriteRule ^offers/ index.php?page=offers&%{QUERY_STRING}
RewriteRule ^cart/ index.php?page=cart&%{QUERY_STRING}
RewriteRule ^orders/ index.php?page=orders&%{QUERY_STRING}
RewriteRule ^addresses/ index.php?page=addresses&%{QUERY_STRING}

why does my rewrite rule without redirect give a 500 error?

i want to rewrite urls like
mysite.com/eng?sentence=cat
to show like:
mysite.com/eng/cat
I dont want a redirect just a rewrite. so I tried altering a rewrite rule i found, the example before i edited it had a R=301 in it originally:
RewriteCond %{THE_REQUEST} ^GET\ /eng\?sentence=([^&\s]+) [NC]
RewriteRule ^eng\?sentence=$ /english/%1? [R=302,NC,L]
but when i remove the R=301 from the 2nd line it gives a 500 error.
how can I write this kind of rewrite rule? Im not great with this regex stuff, nor am i familiar with this
my htaccess file:
# Use PHP5.3 Single php.ini as default
AddHandler application/x-httpd-php54s .php
AddType application/x-httpd-php .htm .html
Options -Indexes -MultiViews
ErrorDocument 404 /404.php
ErrorDocument 500 /500.php
RewriteEngine on
RewriteCond %{THE_REQUEST} ^GET\ /eng\?sentence=([^&\s]+) [NC]
RewriteRule ^eng\?sentence=$ /english/%1? [R=302,NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
<FilesMatch "\.(jpg|jpeg|png|gif|swf)$">
Header set Cache-Control "max-age=604800, public"
</FilesMatch>
Use line end anchor $ OR else just reply on RewriteCond
RewriteCond %{THE_REQUEST} ^GET\s/eng\?sentence=([^&\s]+) [NC]
RewriteRule ^ /eng/%1? [NC,L,R=302]
Most likely 500 is because of looping due to other rules in your .htaccess. I suggest you posting full .htaccess code in your question.

why does my rewrite rule put a question mark at the end?

to rewrite pages like:
mysite.com/eng/cat
to redirect to:
mysite.com/eng?sentence=cat
I made the following rewrite rule:
RewriteCond %{THE_REQUEST} ^GET\ /eng/([^&\s]+) [NC]
RewriteRule ^eng/ eng\?sentence=%1? [R=301,NC,L]
but when i request
mysite.com/eng/cat
it rewrites like this
mysite.com/eng?sentence=cat?
i dont know why it puts a question mark at the end, i also tried
RewriteCond %{THE_REQUEST} ^GET\ /eng/([^&\s]+) [NC]
RewriteRule ^eng/ eng\?sentence=%1 [R=301,NC,L]
and
RewriteCond %{THE_REQUEST} ^GET\ /eng/([^&\s]+) [NC]
RewriteRule ^eng/ eng\?sentence=%1\? [R=301,NC,L]
which dont make any difference. any help?
my htaccess file:
# Use PHP5.3 Single php.ini as default
AddHandler application/x-httpd-php54s .php
RewriteEngine on
RewriteCond %{THE_REQUEST} ^GET\s/+eng/([^/?&\s]+) [NC]
RewriteRule ^ eng?sentence=%1 [R=302,NC,L,NE]
AddType application/x-httpd-php .htm .html
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Options -Indexes
ErrorDocument 404 /404.php
ErrorDocument 500 /500.php
<FilesMatch "\.(jpg|jpeg|png|gif|swf)$">
Header set Cache-Control "max-age=604800, public"
</FilesMatch>
Just this rule should work:
RewriteCond %{THE_REQUEST} ^GET\s/+eng/([^/?&\s]+) [NC]
RewriteRule ^ eng?sentence=%1 [R=302,NC,L,NE]
Make sure to test in a different browser to avoid 301 caching pitfall.
Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.
Suggested htaccess file:
# Use PHP5.3 Single php.ini as default
AddHandler application/x-httpd-php54s .php
AddType application/x-httpd-php .htm .html
Options -Indexes -MultiViews
ErrorDocument 404 /404.php
ErrorDocument 500 /500.php
RewriteEngine on
RewriteCond %{THE_REQUEST} ^GET\s/+eng/([^/?&\s]+) [NC]
RewriteRule ^ /eng?sentence=%1 [R=302,NC,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
<FilesMatch "\.(jpg|jpeg|png|gif|swf)$">
Header set Cache-Control "max-age=604800, public"
</FilesMatch>

Resources