htaccess redirect 301 from URL to URL - .htaccess

I have in my .htaccess file line like this, which is work:
Redirect 301 /redirect/test.php http://example_site/newtest/1-newtest
but I am wondering about that can I use this to redirect from URL to another URL like this:
Redirect 301 https://example_site.pl/test/test/1,test http://example_site.pl/newtest/1-newtest
or it can be redirected only from path to URL?

You can use mod-rewrite to redirect requests based on HTTP_HOST header . To redirect /file.html from a.com to b.com you can use something like the following:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?a\.com$
RewriteRule ^file\.html$ http://b.com/ [L,R]
References:
http://httpd.apache.org/docs/current/mod/mod_rewrite.html

Related

htaccess redirect some partical url 301 SEO

something on our BLOG URLS have changed.
No i want to have some URL path deleted in urls.
Example:
OLD URL:
https://do-main.de/blog/entry/die-xxx-der-portrait
NEW URL:
https://do-main.de/blog/die-xxx-der-portrait
My htaccess attempt to setup:
#OLD BLOG URLS
RewriteEngine On
# anything that is equal to https://do-main.de/blog/entry/*
RewriteCond %{HTTP_HOST} ^do\-main\.de\/blog\/entry\/$
# redirects to https://do-main.de/blog/*
RewriteRule ^/?(.*)$ https://do-main.de/blog/$1 [R=301,L]
conclusion all URLS with "entry" should be only using /blog/ without /entry/
Do you understand? Can anyone help?
You can use Redirect directive.
Redirect 301 /blog/entry/ http://example.com/blog/
This will 301 redirect all requests from example.com/blog/entry/foobar to http:// example.com/blog/foobar .

Htaccess Redirects with Parameters (need to be removded)

For an migration of a new website i need to redirect /example.html?id=2 redirected to /example/new-page.html
When i create an redirect like this:
Redirect 301 /example.html?id=2 https://www.url.com/example/new-page.html
Redirect 301 /example2.html?bla=34 https://www.url.com/example/new-page2.html
Redirect 301 /eteste.html?yolo=2 https://www.url.com/example/new-page3.html
It returned into this:
https://www.url.com/example/new-page.html?id=2
etc
Change it into this doesn't work either:
Redirect 301 /example.html?id=2 https://www.url.com/example/new-page.html?
Is there something i'm doing wrong (yes! ;))
You need to use mod_rewrite to match against the query string :
RewriteEngine on
RewriteCond %{QUERY_STRING} ^id=([^&]+) [NC]
RewriteRule ^example.html$ https://www.url.com/example/new-page.html? [NC,L,R]

Can't use ? when redirect 301 with .htaccess

I have a website which is being renewed.
It has url's like site.com/page.php?p=company&l=nl
The new URL is site.com/company
I would like to use .htaccess to redirect 301.
(I would like to keep the SEO pagerank for the old pages)
Because of the ? it doesn't work.
This is the rule I use in my htaccess which doesn't work:
Redirect 301 /page.php?p=company&l=nl http ://www.site.com/company
This is the rule I use in my htaccess which does work:
Redirect 301 /page.php http ://www.site.com/company
I need the ?p=...
This should work:
RewriteCond %{QUERY_STRING} ^p=company&l=nl$ [NC]
RewriteRule ^page\.php$ http ://www.site.com/company? [R=301,NE,NC,L]
Remove the space between http and :

How can I redirect URL's with numbers using by htaccess?

I need redircet a lot of pages on my site.
all those page url is:
/index.php?act=download&id=9&mirror=0
/index.php?act=download&id=9&mirror=1
/index.php?act=download&id=9&mirror=2
/index.php?act=download&id=10&mirror=0
/index.php?act=download&id=10&mirror=1
Etc.
I need to write in my htaccess file this code:
Redirect 301 /index.php?act=download&id=9&mirror=0 http://mydomain.com
Redirect 301 /index.php?act=download&id=9&mirror=1 http://mydomain.com
Redirect 301 /index.php?act=download&id=9&mirror=2 http://mydomain.com
Redirect 301 /index.php?act=download&id=10&mirror=0 http://mydomain.com
Redirect 301 /index.php?act=download&id=10&mirror=1 http://mydomain.com
Redirect 301 /index.php?act=download&id=10&mirror=2 http://mydomain.com
but I have a lot more..
how can I redirect all of this:
/index.phpact=download&id=ANY-NUMBER&mirror=ANY-NUMBER
to my main page? (http://mydomain.com)
You cant match query string using Redirect directive. Use mod_rewrite instead:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^act=download&id=[0-9]+&mirror=[0-9]+
RewriteRule ^index\.php$ http://mydomain.com? [L,R=301,NC]

How do i redirect the base url using htaccess?

I have a URL structure of:
mysite.com/user/(someusername)
and want to redirect only the base URL which is:
mysite.com/user/
to
mysite.com/users/ or mysite.com/members/
But the problem is when i add this line to htaccess.
redirect 301 /user /users/
Even
mysite.com/user/(someusername)
gets redirected to
mysite.com/users/(someusername)
How can i redirect /user/ only?
Thanks!
RewriteEngine on
RewriteBase /
RewriteRule ^user/$ users/ [L,R]

Resources