.htaccess redirection with multiple rules - php

Basically what I want to do is :
1) redirect any .html request to .php page.
2) if any 404 happens , then it should redirect to www.domain.com
3) domain.com should be redirected to www.domain.com
4) www.domain.com/index.html should redirect to www.domain.com
Here is my htaccess code
RewriteEngine On
RewriteRule ^(.*)\.html$ $1.php [L]
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^fr/(.)*$ / [R=301,NC,L] # Added line
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . http://www.domsdain.com/index.php [L]
</IfModule>
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteRule ^(.*)index\.(php|html?)$ /$1 [R=301,NC,L]
With above code 4th rule (www.domain.com/index.html should redirect to www.domain.com) is not working

You can refactor to use these rules:
ErrorDocument 404 http://www.examle.com/
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \s/+index\.(?:php|html) [NC]
RewriteRule ^ http://www.examle.com/ [L,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
RewriteRule ^(.+)\.html$ $1.php [L,NC]
RewriteRule ^fr/(.)*$ / [R=301,NC,L]
Make sure to clear your browser cache or use a new browser for testing.

Related

Redirect 301 HTACCES

How I can redirect page from https://url.pl?page=page to https://url.eu/page.html
I used on other page and this below works
<IfModule mod_rewrite.c>
Redirect 301 /page.html /newpage.html
</IfModule>
but when I used once below it doesn`t works
<IfModule mod_rewrite.c>
Redirect 301 /page.html /?page=page
</IfModule>
<IfModule mod_rewrite.c>
Redirect 301 https://www.domain.eu/page.html https://www.domain.eu/?page=page
</IfModule>
I also used RewriteRule but it also doesn`t works
htacess file
RewriteEngine on
RewriteBase /
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} https://www.maripol.pl/en/ [NC]
RewriteRule ^(.*)$ https://maripol.pl/en/$1 [L,R=301]
RewriteRule ^index.html$ https://maripol.pl/en [R=301,L]
RewriteRule ^index.php$ https://maripol.pl/en [R=301,L]
RewriteRule ^(.*),more.html$ index.php?page=more&id=$1 [NC,L]
RewriteRule ^(.*),pogodzinach.html$ index.php?page=pogodzinach&id=$1 [NC,L]
RewriteRule ^(.*),(.*).html$ index.php?page=oferta&id=$1&url=$2 [NC,L]
RewriteRule ^(.+)\.html$ ?page=$1 [NC,L]
RewriteCond %{THE_REQUEST} /en/\?page=informacje\s [NC]
RewriteRule ^en/?$ https://maripol.pl/en/informacje.html? [L,R,NE]
You can use RewriteRule
RewriteEngine on
#redirect /?page=page to /page.html
RewriteCond %{THE_REQUEST} /en/\?page=informacje\s [NC]
RewriteRule ^en/?$ https://maripol.pl/en/informacje.html? [L,R,NE]

Wordpress site htaccess not redirecting to https when manually entering URL starting with http

I've made changes to my htaccess file on my WordPress site to redirect traffic from http to https.
Most cases it works fine and redirects traffic to https, but some cases it doesn't.
For example, if I try access home page with http in address it redirects to https, but if I try and access another page on the site with http in URL it stays on http:
http: //example.com redirects fine to https: //example.com
http: //example.com/page stays on http: //example.com/page
Current htaccess:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
</IfModule>
# END WordPress
I've tried numerous answers in other question, what else can I try?
try this
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Why do you have the following at the bottom? What happens when you remove this? I think this may be conflicting with RewriteCond %{HTTPS} off at the top. What happens when you remove this...
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
It will work for www and https
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Change your site address in Settings:
and add the code below at the top of WordPress’ .htaccess file.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^yoursite.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.yoursite.com [NC]
RewriteRule ^(.*)$ https://www.yoursite.com/$1 [L,R=301,NC]
I had the same problem, and the fix was really simple... Just be sure to put the https redirection BEFORE the original rewrite code of Wordpress...
Exemple :
# Redirection code (redirect no www to www and http to https)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^yourdomain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.yourdomain\.com$
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R=301,L]
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

htaccess redirect appending old url to new

Website is using expression engine, trying to add some simple 301 redirects using the below code;
Redirect 301 /contact-us/ http://www.website.co.uk/get-in-touch
This is returning the following url;
http://www.website.co.uk/get-in-touch?contact-us
I cannot find why this is doing this, I have also tried;
RewriteRule ^/contact-us/$ http://www.website.co.uk/get-in-touch [R=301]
RedirectMatch 301 ^/contact-us/?$ http://www.website.co.uk/get-in-touch
Both return the same result, the htaccess as a whole;
<IfModule mod_rewrite.c>
RewriteEngine On
# Removes index.php from ExpressionEngine URLs
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
#enforce the www's
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>
RewriteRule ^/contact-us/$ http://www.website.co.uk/get-in-touch [R=301]
#RedirectMatch 301 ^/contact-us/?$ http://www.website.co.uk/get-in-touch
#Redirect 301 /contact-us/ http://www.website.co.uk/get-in-touch
Your first RewriteRule appears to trick you.
A simple switch in the order should fix it. My general rule of thumb: External redirects first, internal rewrites second.
So in your case:
<IfModule mod_rewrite.c>
RewriteEngine On
# Redirect /contact-us to /get-in-touch
RewriteRule ^contact-us(/?)$ /get-in-touch [L,R=301]
# Removes index.php from ExpressionEngine URLs
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
#enforce the www's
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>

301 redirect in htacess for default language

I have wordpress site with qtranslate plugin.
Russian language is default (ru).
I have pages:
http://example.com/en/xxx
http://example.com/ru/xxx (302 redirect to http://example.com/xxx)
http://example.com/xxx
I need to create 301 redirect from http://example.com/ru/xxx to http://example.com/xxx
I tried to write rule in htacess for redirection, but I got redirect loop:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html\ HTTP/
RewriteRule ^index\.html$ http://example.com.ua/ [R=301,L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://example.com.ua/ [R=301,L]
RewriteCond %{HTTP_HOST} ^www.example\.com.ua$ [NC]
RewriteRule ^(.*)$ http://example.com.ua/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !^/ru(/|$)
RewriteRule ^(.*)$ /$1 [R=301]
</IfModule>
# END WordPress
Try below rule:-
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /ru/$1 [NC,L,QSA]
OR
RewriteEngine On
RewriteRule ^ru/(.*)$ /$1 [L,R=301,QSA]
Hope it will work for you :)

.Htaccess redirect index.php

I transfered an old bulletin board to q2a.org. Now i have a lot index.php files in google which do as 200 OK Status. How can I redirect those links to root? ex. www.domain.com/index.php?page=Board&boardID=14 or www.domain.com/index.php?page=Thread&postID=58585
This is my current .htaccess file
ErrorDocument 404 /404.php
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteBase /
# Redirect non-www to www:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php?qa-rewrite=$0&%{QUERY_STRING} [L]
</IfModule>
Thanks
Insert this rule just after RewriteBase line:
RewriteCond %{QUERY_STRING} page=[^&]+&postID=[^&]+ [NC]
RewriteRule ^ /? [R=301,L]

Categories