301 redirects dynamic URL's htacces - apache

I'm having trouble figuring 301 redirecting this.
Old site has a structure like this for subcategories:
www.domain.com/?someid=1&pageload=serieslist&someparam=1
What I would like to do is redirect some of the subcategories to new categories 1:1, and then the rest to new domain root.
Will it work doing it like this, first listing all 1:1 redirects, and then a "catch all" for the rest?
RewriteEngine On
RewriteBase /
Redirect 301 /?someid=1&pageload=serieslist&someparam=1 http://www.newdomain.com/category1/
Redirect 301 /?someid=2&pageload=serieslist&someparam=1 http://www.newdomain.com/category2/
RewriteCond %{QUERY_STRING} ^pageload=serieslist$ [NC]
RewriteRule ^(.*)$ http://www.newdomain.dk/ [R=301,L]

You cannot use Redirect directive to match query string. Use it like this:
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} someid=1&pageload=serieslist&someparam=1 [NC]
RewriteRule ^$ http://www.newdomain.com/category1/? [L,R=302]
RewriteCond %{QUERY_STRING} someid=2&pageload=serieslist&someparam=1 [NC]
RewriteRule ^$ http://www.newdomain.com/category2/? [L,R=302]
RewriteCond %{QUERY_STRING} (^|&)pageload=serieslist(&|$) [NC]
RewriteRule ^ http://www.newdomain.dk/? [R=301,L]

Related

htaccess: rewrite and redirect 301

I rewrited, by .htaccess, a category of dynamic url generated by a query string in this mode:
RewriteEngine On
RewriteRule ^id1-([^-]*)-id2-([^-]*)$ /page.php?id1=$1&id=$2 [L]
Now my rewrite works in right way and, for example, the following urls drive to the same page:
http://www.mysite.it/id1-01234-id2-56789
http://www.mysite.it/page.php?id1=01234&id2=56789
But now I want a redirect 301, from second type to first type, for all dynamic urls. For example:
from
http://www.mysite.it/page.php?id1=01234&id2=56789
to
http://www.mysite.it/id1-01234-id2-56789
The following way doesn't work:
RewriteEngine On
RewriteRule ^id1-([^-]*)-id2-([^-]*)$ /page.php?id1=$1&id=$2 [L]
RewriteCond %{QUERY_STRING} (^|&)id1=$1($|&)
RewriteCond %{QUERY_STRING} (^|&)id2=$2($|&)
RewriteRule ^page\.php$ /id1-id2? [L,R=301]
Where is the error?
can you help me please?
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/page.php
RewriteCond %{THE_REQUEST} \?id1=(\w+)&id2=(\w+)\s
RewriteRule ^page.php /id1-%1-id2-%2? [NC,R=301,L]
RewriteRule ^id1-([^-]*)-id2-([^-]*)$ /page.php?id1=$1&id2=$2 [L]

htaccess rewrite condtion only for specific domain not sub folder

My current code is this
Redirect 301 /index.html http://example.com/
Redirect 301 /about.html http://example.com/
Redirect 301 /contact.html http://example.com/contact.php
However this is affecting another domain that points to a sub folder of this site.
is there a way to say something like if(DOMAIN != 'example2.com'){...}
i also use other RewriteRule functions like this
RewriteRule ^events/?$ events_page.php [L]
and also this to remove www
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
Can't think of a particularly elegant way, but the following should do the job:
RewriteCond %{HTTP_HOST} !^example2\.com
RewriteRule /(index|about)\.hmtl http://example.com/ [R=301,L]
RewriteCond %{HTTP_HOST} !^example2\.com
RewriteRule /contact\.hmtl http://example.com/contact.php [R=301,L]

htaccess redirects the domain, but not individual pages?

I have a website that has changed domains. I have 4 specific pages that need to be redirected to their new equivalents on the new domain. Other than that, I want the entire domain to redirect to the new one.
Here is my code:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
# Redirect individual pages.
RewriteRule http://olddomain.net/subdirectory/subpage1/$ https://www.newdomain.com/newsub/newpage1/ [R=301,L]
RewriteRule http://olddomain.net/subdirectory/subpage2/$ https://www.newdomain.com/newsub/newpage2/ [R=301,L]
RewriteRule http://olddomain.net/subdirectory/subpage3/$ https://www.newdomain.com/newsub/newpage3/ [R=301,L]
RewriteRule http://olddomain.net/subdirectory/subpage4/$ https://www.newdomain.com/newsub/newpage4/ [R=301,L]
RewriteRule (.*) http://www.newdomain.com/ [R=301,L]
I have also tried writing the RewriteRules like this, but it has the same effect:
RewriteRule ^/subdirectory/subpage1/$ https://www.newdomain.com/newsub/newpage1/ [R=301,L]
This only seems to perform the last line, that is, it always redirects to http://www.newdomain.com/, regardless of what olddomain.net URL was entered. How do I get the individual URLs to redirect?
SOLVED: Here is what I ended up using:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/subdirectory/subpage1/$
RewriteRule ^.*$ https://www.newdomain.com/newsub/subpage1/ [R=301,L]
RewriteCond %{REQUEST_URI} ^/subdirectory/subpage2/$
RewriteRule ^.*$ https://www.newdomain.com/newsub/subpage2/ [R=301,L]
RewriteCond %{REQUEST_URI} ^/subdirectory/subpage3/$
RewriteRule ^.*$ https://www.newdomain.com/newsub/subpage3/ [R=301,L]
RewriteCond %{REQUEST_URI} ^/subdirectory/subpage4/$
RewriteRule ^.*$ https://www.newdomain.com/newsub/subpage4/ [R=301,L]
Redirect 301 / http://www.newdomain.com/
the RewriteRule operates on Path not full Uri, the second method you tried works fine, just remove the leading forward slash:
RewriteRule ^subdirectory/subpage1/?$ https://www.newdomain.com/newsub/newpage1/ [R=301,L]
RewriteRule ^subdirectory/subpage2/?$ https://www.newdomain.com/newsub/newpage2/ [R=301,L]
RewriteRule ^subdirectory/subpage3/?$ https://www.newdomain.com/newsub/newpage3/ [R=301,L]
RewriteRule ^subdirectory/subpage4/?$ https://www.newdomain.com/newsub/newpage4/ [R=301,L]
RewriteRule (.*) http://www.newdomain.com/ [R=301,L]

Redirection with Apache2 query_string with square brackets

I have a URL like
http://domain.net/documents?f[author]=43
Which should be redirected to
http://domain.net/documents/author/43
What I have so far is
RewriteEngine On
Rewritebase /documents
RewriteCond %{query_string} f([^/]+)=(.*)$ [NC]
RewriteRule (.*) author/%2? [R=301,L]
However, this redirects all URLs, including
http://domain.net/documents/author/43?page=1
back to
http://domain.net/documents/author/43
when I just need to replace the "?f[author]=" part with "/author/"
RewriteEngine On
Rewritebase /documents
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{query_string} f([^/]+)=(.*)$ [NC]
RewriteRule (.*) author/%2? [R=301,L]

Apache redirect subdomain to folder, keep parameters

I have this code in .htaccess :
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.(css|gif|ico|jpg|js|png|swf|txt)$
# If empty subdomain, replace with "www"
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^(.*) http://www.example.com/$1 [QSA,L,R=301]
# If subdomain isn't empty and not "www", redirect to "folder"
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com$
RewriteRule (.*) http://www.example.com/%1/$1 [QSA,R=301]
#PAGES REDIRECTION
RewriteRule ^(.*)/register/ /index.php?sub=$1&page=register
RewriteRule ^(.*)/register /index.php?sub=$1&page=register
RewriteRule ^(.*)/lostpass/ /index.php?sub=$1&page=lostpass
RewriteRule ^(.*)/lostpass /index.php?sub=$1&page=lostpass
...
(a rule for wildcard subdmains is already in place and working)
If I browse to http://test.example.com it redirects correctly to http://www.example.com/test but when I try to browse to http://test.example.com/register, it actually redirect to http://www.example.com/test/index.php?sub=http://www.example.com/test&page=register which should redirect to http://www.example.com/test/register
What am I doing wrong here? Thanks in advance!
Try adding the L flag to your second redirect rule, similar to how have it in the first.
RewriteRule (.*) http://www.example.com/%1/$1 [QSA,R=301,L]
It looks like your rewritten URI is passing through to the next rule.
Also, I don't think your first two RewriteCond are in the correct spot.