.htaccess internal redirect not working - regex

I tried doing some .htaccess redirects for internal pages but they are not working for me. This is my .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.domain\.com
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
RewriteRule articles articles/how-to-play-piano [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteRule . index.php [L]
</IfModule>
I also tried this outside <IfModule> but not working:
Redirect 301 http://www.domain.com/articles http://www.domain.com/articles/how-to-play-piano
and
Redirect 301 /articles http://www.domain.com/articles/how-to-play-piano

Your regex is wrong for articles rule and will cause infinite looping. To fix that you need to use anchors ^ and $:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.domain\.com
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
RewriteRule ^articles/?$ articles/how-to-play-piano [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteRule . index.php [L]

Related

domain redirection in htaccess file

I want to redirect my domain landing page to other domain but I don't want to Redirect Everything After the Domain landing page.Can any one help me How I can do this. my htaccess file :-
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
RewriteCond %{HTTP_HOST} ^women\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.women\.com$
RewriteRule ^/?$ "http\:\/\/beta\.women\.com\/" [R=301,L]
I want women.com landing page redirect to beta.women.com but women.com?post_type=in_product&p=509&preview=1 or women.com/about or everything after women.com/ not to redirect to beta.women.com
any help will be highly appreciated !
Thank you !
Keep redirect rule before other rules and exclude any query string from redirect using a RewriteCond:
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{HTTP_HOST} ^(?:www\.)?(women\.com)$ [NC]
RewriteRule ^/?$ http://beta.%1/ [R=301,L]
RewriteRule ^index\.php$ - [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]

Redirect without get-params

I need to remove get params from url-string.There is code from .htaccess:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://formaspravki.com? [R=301,L]
RewriteCond %{HTTP_HOST} ^www.formaspravki\.com
RewriteRule ^(.*)$ http://formaspravki.com/$1? [R=permanent,L]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.forma-spravki\.com
RewriteRule ^(.*)$ http://formaspravki.com/$1? [R=permanent,L]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^forma-spravki\.com
RewriteRule ^(.*)$ http://formaspravki.com/$1? [R=permanent,L]
DefaultLanguage ru
AddDefaultCharset windows-1251
#php_value default_charset "cp1251"
# Редиректы
RewriteRule ^page/([0-9]+)(/?)$ index.php?cstart=$1? [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^.]+)/page/([0-9]+)(/?)+$index.php?do=cat&category=$1&cstart=$2[L]
RewriteRule ^([^.]+)/?$ index.php?do=cat&category=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)/rss.xml$ engine/rss.php?do=cat&category=$1 [L]
RewriteRule ^page,([0-9]+),([^\/]+).html$ index.php?do=static&page=$2&news_page=$1 [L]
RewriteRule ^print:([^\/]+).html$ engine/print.php?do=static&page=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\/]+).html$ index.php?do=static&page=$1 [L]
Redirect 301 /spravka_rabotodatelyu_o_beremennosti.html http://formaspravki.com/spravka-o-beremennosti.html
Redirect 301 /spravka_o_beremennosti_obrazec.html http://formaspravki.com/spravka-o-beremennosti.html
Afer redirecting, in url string appears parametrs ?do=static&page='page name'.
When I add next code paramets disappering,but content in browser isn`t loading:
RewriteCond %{QUERY_STRING} !="^do=static$"
RewriteRule ^(.*)$ http://example.com/$1? [L]
Adding '?' to the directive or deleting query string didnt help.
Try appending [QSD] flag to your url, for example:
RewriteRule ^index\.php$ http://example.com [R=301,L,QSD]
Problem is solved!
RewriteCond %{QUERY_STRING} "^do=static&page=old-name$"
RewriteRule ^([^\/]+)$ new-link$1? [R=permanent,L]

www to non-www with removing extensions htaccess

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.

.htaccess - 301 redirect move all pages to https:// - except 1 page?

I have the following code that redirects any http:// request to https:// - This work great, but how would I edit this to make an exception for one page e.g. mydomain.com/sitemap-news.xml - and keep this as http:// ?
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.mydomain.com/$1 [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
As listed in comments here is how my entrie .htaccess looks
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule !^sitemap-news\.xml$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{HTTPS} off
RewriteRule !^sitemap-news\.xml$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE,NE]
You can make an exception for sitemap file:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule !^sitemap-news\.xml$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{HTTPS} off
RewriteRule !^sitemap-news\.xml$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE,NE]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

redirecting url containing specific string

I'm trying to redirect all urls that contain a specific string. The urls look like this
http://www.domain/com/modules.php?name=Kalender&op=list&d=8&m=6&y=2034
I have to redirect all urls that contain
name=Kalender&
to
http://www.domain.com/kalender/
I tried several rules in my .htaccess. None of them worked:
RewriteCond %{REQUEST_URI} name=Kalender&
RewriteRule .* kalender
RewriteRule ^(.)name=Kalender&(.)$ http://www.domain.com/kalender/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/name=Kalender&/i$ http://www.domain.com/kalender/ [NE,L]
RewriteCond %{REQUEST_URI} name=Kalender&
RewriteRule ^(.+)$ http://www.domain.com/kalender/ [L,R=301]
RewriteCond %{QUERY_STRING} ^name=Kalender&
RewriteRule ^name=Kalender& http://www.domain.com/kalender/ [R=301,L]
RewriteRule ^(.*)name=Kalender&(.*)$ http://www.domain.com/kalender/ [L,R=301]
RewriteCond %{REQUEST_URI} ^/modules.php?name=Kalender&$
RewriteRule ^(.*) http://www.domain.com/kalender [R=301,L]
This is the WordPress .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Any help would be appreciated!
You can use this rule as your first rule:
RewriteCond %{QUERY_STRING} (^|&)name=Kalender(&|$) [NC]
RewriteRule ^ /kalender/? [L,R]