.htaccess main page - .htaccess

I'm trying to redirect any non-existing URL back to its main page.
Below is my .htaccess code:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.)?example\.com$ [NC]
RewriteRule (.*) http://www.example.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteRule ^(.+?)/$ /$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]

Add that:
RewriteRule ^([^.]+\.[^/.]{3,4})/.* $1 [R=301,L]
And no need to use more than one RewriteEngine on for all the .htaccess.

Related

How to redirect a URL and then rewrite it to same url with htaccess but from the root?

This code below for rewrite/redirect from localhost/moneyworld/exchange?title=BTC_PMUSD to localhost/moneyworld/BTC_PMUSD but i want to rewrite/redirect from www.domain.com/exchange?title=BTC_PMUSD to www.domain.com/BTC_PMUSD Without also /moneyworld/
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s/(moneyworld)/exchange\?title=([^\s]*)\s [NC]
RewriteRule ^ /%1/%2 [NE,QSD,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/?$ /moneyworld/exchange?title=$2 [L]
Thanks
The problem was that i was forwarding exchange.php to exchange after the url rewrite rules so simply the code below solved it
RewriteEngine ON
RewriteBase /
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{THE_REQUEST} \s/exchange\?title=([^\s]*)\s [NC]
RewriteRule ^ /%1 [NE,QSD,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ /exchange?title=$1 [L]

How do I Redirect on Https to another Https?

I'm trying to make a redirect from one domain to another. I want to redirect in all cases to "https://myweb.com" using htaccess file. Today I have the following code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.myweb.com.ar [OR]
RewriteCond %{HTTP_HOST} ^myweb.com.ar$
RewriteRule ^(.*)$ https://www.myweb.com/ [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Let's see the results:
1) http://www.myweb.com >> OK!
2) http://www.myweb.com.ar >> OK!
3) https://www.myweb.com.ar >> ERROR, The redirect is not happenning here.
What can I try to solve this issue?
Tks in advance!
Try this rewrites, domain ar->com, http->https, www->non www, request->filename.php
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?myweb\.com\.ar [NC]
RewriteRule (.*) https://myweb.com/ [R=301,L]
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://myweb.com%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^www.myweb.com$ [NC]
RewriteRule ^(.*)$ https://myweb.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

htaccess rewrite get url section not working

I am trying to rewrite the url example.com/parts/toolbucket/part.php?id=1 into example/toolbucket/part/1
I have most of the .htaccess working but the last part of the rewrite doesn't work.
This is my .htaccess
Options -Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} \s/parts/([^\s]+)\s [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /parts/$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
RewriteRule ^toolbucket/([0-9]*)$ ./part.php?id=$1
Any ideas?
Because you are missing the directory part in the rule. If you want your URL to be this example.com/toolbucket/part/1
Replace this
RewriteRule ^toolbucket/([0-9]*)$ ./part.php?id=$1
With this.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^toolbucket/part/([0-9]*)/?$ /parts/toolbucket/part.php?id=$1 [L]

301 redirection through .htaccess file not working

I'm using CodeIgniter.
in httacess file, I wrote this code to remove index.php from url:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
Now I want to redirect site root to subdomain, with this code:
rewritecond %{http_host} ^domain.com [nc]
rewriterule ^(.*)$ sub.domain.com/$1 [r=301,nc]
domain.com redirected to sub.domain.com successfully. but domain.com/dir to sub.domain.com/dir not work and still opening domain.com/dir.
When I removed first code, redirection worked perfect, but I need to remove index.php too.
Adding $to the end of rewritecond %{http_host} ^domain.com [nc] should fix it for you.
rewritecond %{http_host} ^domain.com [nc]
rewriterule ^(.*)$ sub.domain.com/$1 [r=301,nc]
Would become
rewritecond %{http_host} ^domain.com$ [nc]
rewriterule ^(.*)$ sub.domain.com/$1 [r=301,nc]
Have your rules like this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://sub.domain.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]

Htaccess redirect with many rules

I got rewrite rules:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www|static)\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301] //add www before adress
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC] //remove .php
RewriteRule ^ %1 [R=301]
RewriteCond %{REQUEST_FILENAME} !-d //remove .php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php
RewriteRule ^portfolio/([0-9]+)([^.]*)$ /portfolio-single.php?id=$1 [L]
But the last one dosent work, i want to have adress like www.mysite.com/portfolio/3-some-name => /portfolio-single.php?id=3
I dont know how to get i work.
(Sorry for my bad english)
Change order of rules:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www|static)\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{THE_REQUEST} \s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
RewriteRule ^portfolio/([0-9]+) /portfolio-single.php?id=$1 [L,NC,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]

Resources