How to correct set up rewrite rule? - .htaccess

I have this setting for my website for SEO-friendly URL, it's working correct. But when I need set some 301 Redirect it works poorly. When I type in address bar that rule below Redirect 301 /example1 /example2 I get www.domain.com/example2?page=example1. How can I change it?
RewriteEngine on
RewriteRule ^([^/.]+)/?$ index.php?page=$1 [L]
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Redirect 301 /example1 /example2

You should keep using mod_rewrite since you're already using it. And put the rule above the other one.
RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteRule ^example/? /example2 [L,R=301]
#redirect to index.php except for example2
RewriteCond %{REQUEST_URI} !^/example2
RewriteRule ^([^/.]+)/?$ index.php?page=$1 [L]

Related

.htaccess multiple domain redirect to once https without www

i have few domains:
www.xxx.com
xxx.com
www.xxx.co
xxx.co
www.xx.io
xxx.io
how to redirect all domains to https://xxx.io?
I have a redirect to https without www:
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Use this rule in your .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.)?example.io [NC]
RewriteRule ^(.*)$ https://example.io%{REQUEST_URI} [L,R=301]
The condition checks to see if the URL is either www.example.io or exmaple.io and if it isn't, then it will rewrite to it.
I've used R=301 which is a permanent redirection. For testing purposes, I advise you change this to R or R=302, which is temporary.
Make sure you clear your cache before testing this.
you can add the following lines to your .htaccess file located at the root of your xxx.io domain:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^xxx.com$ [OR]
RewriteCond %{HTTP_HOST} ^xxx.co$
RewriteRule (.*)$ http://xxx.io/$1 [R=301,L]
</IfModule>

Http with .html to https without html in one redirect

I'm trying rewrite our website's htaccess file. I'm not too good with htaccess but I'm studying our existing .htaccess file, right now what I understand is that we have htaccess directives that redirects http to https.
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
And another right below that redirects from a request having a .html extension into no .html.
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteRule ^(.*)\.html$ /$1 [R=301,L]
I'm not sure, but does R=301 mean redirect?
If so, is it doing a redirect after each match in the rewritecond?
And is there a way that I can create another directive that matches both conditions before trying to redirect to both rules?
You can use a single redirect rule for both 301 redirects:
RewriteEngine On
RewriteCond %{THE_REQUEST} \.html\s [NC,OR]
RewriteCond %{HTTPS} !=on
RewriteRule ^(.+?)(?:\.html)?$ https://%{HTTP_HOST}/$1 [R=301,NC,NE,L]
You can use:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteRule ^(.*)\.html$ https://%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

htaccess rewrite rule - vBulletin to SMF

I've recently migrated a forum from vBulletin to SMF and I'm having trouble creating a RewriteCond/RewriteRule that converts the URL http://www.fjcc.com.au/showthread.php?t=3133&page=2 to the URL http://fjcc.com.au/forum_fjcc/index.php?topic=3133.0.
The .htaccess file in the root directory looks like this:
RewriteEngine On
# SMF Rewrite rules for vBulletin links after vBSEO removed
RewriteCond %{QUERY_STRING} ^t=([0-9]+)&page=[0-9]+/?$
RewriteRule ^showthread.php$ /forum_fjcc/index.php?topic=%1.0 [L,R=301]
RewriteCond %{QUERY_STRING} ^t=([0-9]+).*&p=([0-9]+)/?$
RewriteRule ^showthread.php$ /forum_fjcc/index.php?topic=%1.msg%2#msg%2 [L,R=301]
RewriteCond %{QUERY_STRING} ^t=([0-9]+)/?$
RewriteRule ^showthread.php$ /forum_fjcc/index.php?topic=%1.0 [L,R=301]
RewriteCond %{QUERY_STRING} ^f=([0-9]+)&page=[0-9]+/?$
RewriteRule ^forumdisplay.php$ /forum_fjcc/index.php?board=%1.0 [L,R=301]
RewriteCond %{QUERY_STRING} ^f=([0-9]+)/?$
RewriteRule ^forumdisplay.php$ /forum_fjcc/index.php?board=%1.0 [L,R=301]
RewriteCond %{QUERY_STRING} ^do=newthread.f=([0-9]+)/?$
RewriteRule ^newthread.php$ /forum_fjcc/index.php?board=%1.0 [L,R=301]
RewriteCond %{QUERY_STRING} ^r=author/([0-9]+)-.*/?$
# SMF Rewrites for cms directory
RewriteRule ^cms/list.php$ /forum_fjcc/index.php?action=profile;u=%1 [L,R=301]
RewriteCond %{QUERY_STRING} ^t=([0-9]+)&page=[0-9]+/?$
# SMF Rewrites for forum directory
RewriteCond %{QUERY_STRING} ^t=([0-9]+)&page=[0-9]+/?$
RewriteRule ^forum/showthread.php$ /forum_fjcc/index.php?topic=%1.0 [L,R=301]
RewriteCond %{QUERY_STRING} ^t=([0-9]+).*&p=([0-9]+)/?$
RewriteRule ^forum/showthread.php$ /forum_fjcc/index.php?topic=%1.msg%2#msg%2 [L,R=301]
RewriteCond %{QUERY_STRING} ^t=([0-9]+)/?$
RewriteRule ^forum/showthread.php$ /forum_fjcc/index.php?topic=%1.0 [L,R=301]
RewriteCond %{QUERY_STRING} ^f=([0-9]+)&page=[0-9]+/?$
RewriteRule ^forum/forumdisplay.php$ /forum_fjcc/index.php?board=%1.0 [L,R=301]
RewriteCond %{QUERY_STRING} ^f=([0-9]+)/?$
RewriteRule ^forum/forumdisplay.php$ /forum_fjcc/index.php?board=%1.0 [L,R=301]
RewriteCond %{QUERY_STRING} ^do=newthread.f=([0-9]+)/?$
RewriteRule ^forum/newthread.php$ /forum_fjcc/index.php?board=%1.0 [L,R=301]
# SMF Rewrite rules for vBulletin links before vBSEO removed
RewriteRule ^f[\d]+/.+-([\d]+).*/index([\d]+).html/?$ /forum_fjcc/index.php?topic=$1.0 [L,R=301]
RewriteRule ^f([\d]+)/index([\d]+).html/?$ /forum_fjcc/index.php?board=$1.0 [L,R=301]
RewriteRule ^f[\d]+/.+-([\d]+)-post([\d]+)/?$ /forum_fjcc/index.php?topic=$1.msg$2#msg$2 [L,R=301]
RewriteRule ^f[\d]+/.+-([\d]+).*/?$ /forum_fjcc/index.php?topic=$1.0 [L,R=301]
RewriteRule ^f([\d]+)/?$ /forum_fjcc/index.php?board=$1.0 [L,R=301]
RewriteRule ^archive/index.php/t-([\d]+).*html/?$ /forum_fjcc/archive2.php?topic=$1.0 [L,R=301]
RewriteRule ^archive/index.php/f-([\d]+).*html/?$ /forum_fjcc/archive2.php?board=$1.0 [L,R=301]
RewriteRule ^archive/index.php/?$ /forum_fjcc/archive2.php [L,R=301]
RewriteRule ^members/([\d]+).html/?$ /forum_fjcc/index.php?action=profile;u=$1 [L,R=301]
Redirect 301 /index.php /forum_fjcc/
Redirect 301 /forum/showthread.php /forum_fjcc/index.php
Redirect 301 /showthread.php /forum_fjcc/index.php
Redirect 301 /blog/entry.php /forum_fjcc/index.php
Redirect 301 /entry.php /forum_fjcc/index.php
Redirect 301 /blog/blog.php /forum_fjcc/index.php
Redirect 301 /blog.php /forum_fjcc/index.php
Redirect 301 /cms/content.php /forum_fjcc/index.php
Redirect 301 /forum/ /forum_fjcc/index.php
According to the MWL htaccess Tester the rules should return the URL I listed above but that isn't happening - I'm getting /forum_fjcc/index.php?t=3133&page=2 (prefixed by the website URL) instead.
I've been working on this issue for more than a week and I've tried every RewriteCond/RewriteRule combination I've found without any success - hoping someone can point me in the right direction.
I finally sorted this out for myself (and successfully consolidated a few RewriteCond/RewriteRule sets in the process)
The problem was the 'Redirect 301 /showthread.php /forum_fjcc/index.php' statement (although I don't understand why because the statement to rewrite the URL occurred earlier in the file and had the flags [L,R=301] at the end).
My working .htacecss file now looks like this:
# SMF rewrites for vBulletin after vBSEO removed (including forum directory)
RewriteCond %{QUERY_STRING} ^t=([0-9]+)&page=[0-9]+&p=([0-9]+)#[0-9]+(.*)?/?$ [OR]
RewriteCond %{QUERY_STRING} ^t=([0-9]+)&page=[0-9]+&p=([0-9]+)(.*)?/?$
RewriteRule ^(forum)?/?showthread.php$ /forum_fjcc/index.php?topic=%1.msg%2#msg%2 [L,R=301]
RewriteCond %{QUERY_STRING} ^t=([0-9]+)&page=[0-9]+(.*)?/?$ [OR]
RewriteCond %{QUERY_STRING} ^t=([0-9]+)(.*)?/?$
RewriteRule ^(forum)?/?showthread.php$ /forum_fjcc/index.php?topic=%1.0 [L,R=301]
RewriteCond %{QUERY_STRING} ^f=([0-9]+)&page=[0-9]+(.*)?/?$ [OR]
RewriteCond %{QUERY_STRING} ^f=([0-9]+)(.*)?/?$
RewriteRule ^(forum)?/?forumdisplay.php$ /forum_fjcc/index.php?board=%1.0 [L,R=301]
RewriteCond %{QUERY_STRING} ^do=newthread.f=([0-9]+)(.*)?/?$
RewriteRule ^(forum)?/?newthread.php$ /forum_fjcc/index.php?board=%1.0 [L,R=301]
# SMF rewrites for vBulletin before vBSEO removed
RewriteRule ^f[\d]+/.+-([\d]+).*/index([\d]+).html/?$ /forum_fjcc/index.php?topic=$1.0? [L,R=301]
RewriteRule ^f([\d]+)/index([\d]+).html/?$ /forum_fjcc/index.php?board=$1.0? [L,R=301]
RewriteRule ^f[\d]+/.+-([\d]+)-post([\d]+)/?$ /forum_fjcc/index.php?topic=$1.msg$2#msg$2? [L,R=301]
RewriteRule ^f[\d]+/.+-([\d]+).*/?$ /forum_fjcc/index.php?topic=$1.0? [L,R=301]
RewriteRule ^f([\d]+)/?$ /forum_fjcc/index.php?board=$1.0? [L,R=301]
RewriteRule ^archive/index.php/t-([\d]+).*html/?$ /forum_fjcc/archive2.php?topic=$1.0? [L,R=301]
RewriteRule ^archive/index.php/f-([\d]+).*html/?$ /forum_fjcc/archive2.php?board=$1.0? [L,R=301]
RewriteRule ^archive/index.php/?$ /forum_fjcc/archive2.php? [L,R=301]
RewriteRule ^members/([\d]+).html/?$ /forum_fjcc/index.php?action=profile;u=$1? [L,R=301]
# SMF rewrite for cms author
RewriteCond %{QUERY_STRING} ^r=author/([0-9]+)-.*/?$
RewriteRule ^cms/list.php$ /forum_fjcc/index.php?action=profile;u=%1? [L,R=301]
# SMF rewrite for calendar
RewriteRule ^calendar\.php /forum_fjcc/index.php?action=calendar? [L,R=301]
# Other rewrites
Rewrite ^blog\.php /forum_fjcc/index.php? [L,R=301]
Rewrite ^index\.php /forum_fjcc/index.php? [L,R=301]
Rewrite ^entry\.php /forum_fjcc/index.php? [L,R=301]
Rewrite ^showthread\.php /forum_fjcc/index.php? [L,R=301]
Rewrite ^blog/blog\.php /forum_fjcc/index.php? [L,R=301]
Rewrite ^blog/entry\.php /forum_fjcc/index.php? [L,R=301]
Rewrite ^cms/content\.php /forum_fjcc/index.php? [L,R=301]
Rewrite ^forum/ /forum_fjcc/index.php? [L,R=301]

301 several (but not all) http to https

I have a site of about 1.000 static pages, and I would like to see how moving from http to https will effect ranking for, say, 50 pages before I move the entire site.
Do I have to use the first code example below or is it enough with the second one? Or is there a better way to do it?
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^/page-1.htm/?.*$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^/page-2.htm/?.*$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^/page-3.htm/?.*$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# OR IS THIS ENOUGH:
Redirect 301 /page-1.htm https://www.example.com/page-1.htm
Redirect 301 /page-2.htm https://www.example.com/page-2.htm
Redirect 301 /page-3.htm https://www.example.com/page-3.htm
You can use a single rule to redirect multiple pages of this format page-n.htm
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^page-([0-9]+).htm$ https://%{HTTP_HOST}/page-$1.htm [NE,L,R=301]
If you explicitly want it in separate RewriteRule statements:
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^page-1\.htm$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301]
RewriteCond %{HTTPS} off
RewriteRule ^page-2\.htm$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301]
RewriteCond %{HTTPS} off
RewriteRule ^page-3\.htm$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301]

htaccess exclude specific folder

for a website I need a redirect from
domain without www TO domain with www
and
http TO https
The final URL has to be always https://www.myshop.com ...
I solved this successfully with this commands:
# if http, redirect to https
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# if without www, redirect to www (301)
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
The problem ist that I have to exclude a specific folder.
The URL to exclude is: http://www.mydomain.de/myfolder/filename.php
This means: If this URL is requestet there mustn't be a redirect.
Other tutorials I found did not work, sorry :-(
Thanks for any help and best regards...
You can have a skip redirect rule before these 2 redirect rules you have:
RewriteEngine On
# skip myfolder/filename.php from any redirects below
RewriteCond %{THE_REQUEST} /myfolder/filename\.php[?/\s] [NC]
RewriteRule ^ - [L]
# if http, redirect to https
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# if without www, redirect to www (301)
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
Maybe something like that
RewriteEngine on
# if http, redirect to https
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# if without www, redirect to www (301)
RewriteRule ^http://www.mydomain.de/myfolder/filename.php$ - [L]
RewriteRule ^.*\filename.php$ http://www.mydomain.de/myfolder/filename.php [R=301,L]
Adding something like this as a first ruleset may help. (May want to revisit those [L,R] options though.
# check if myfolder is contained
RewriteCond %{REQUEST_URI} ^[^/]*/myfolder(/.*)?$
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# if http, redirect to https
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# if without www, redirect to www (301)
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]

Resources