htaccess 301 redirection not working - .htaccess

In my site I want to redirect with http://domain_name.com/blog to http://domain_name.com/news and it should be work with the following criteria also
http://domain_name.com/blog/2012/06/06//blog_title to http://domain_name.com/news
http://domain_name.com/blog/blog/?cat=2 to http://domain_name.com/news
How would I do this?

Can't test that, but anyway...
RewriteEngine on
RewriteRule /
RewriteRule ^/blog/.*$ /news/ [L,R=301]

Related

Redirect url with language variable to different url htaccess

I need to make the following redirection using htaccess (WordPress)
http://oldweb.com/pl/
to https://newweb.com/url/differenturl/
But this redirection should work only for http://oldweb.com/pl/, the urls http://oldweb.com/en/ or http://oldweb.com/il/ should not have any redirection.
My solution works but it works for /en/ and /il/ also:
Redirect 301 http://oldweb.com/pl/ https://newweb.com/url/differenturl/
I'm not a hero in rewriting, sorry :) Can anyone help me with this simple thing?
Redirect matches only REQUEST_URI not full URL with domain protocol etc.
Better to use a RewriteRule just below RewriteEngine On line in your root .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?oldweb\.com$ [NC]
RewriteRule ^pl/?$ https://newweb.com/url/differenturl/ [L,NC,R=301]

Htaccess redirect always keeps the old path

I would like to make a redirect using htaccess rules to point ALL links from
mywebsite.com/subfolder1/subfolder2/...whatever-path....
to
mywebsite.com/subfolder1
But the problem is that whatever rule I use it always keeps the old path in the destination link, for example:
mywebsite.com/subfolder1/subfolder2/mypage.php
becomes
mywebsite.com/subfolder1/mypage.php
I tried all the following possibilities:
RewriteRule (.*) https://mywebsite.com/subfolder1/$1 [R=301,L]
RewriteRule ^subfolder1/subfolder2/(.*)$ /subfolder1/$1 [R=301,NC,L]
RewriteRule ^/subfolder1/subfolder2/(.*)$ /subfolder1/$1 [R=301,NC,L]
Redirect 301 /subfolder1/subfolder2 https://mywebsite.com/subfolder1
RedirectMatch 301 /subfolder1/subfolder2(.*)$ /subfolder1/$1
RedirectMatch 301 ^/subfolder1/subfolder2/?$ https://mywebsite.com/subfolder1
I have placed the "Options +FollowSymLinks" and "RewriteEngine on" rules before them, but NONE of them worked.
Can someone tell what's wrong and how to find the right rule?
Thank you.
Try this
RewriteRule ^subfolder1/subfolder2/(.*)$ /subfolder1 [R=301,NC,L]
With $1you attach the content of (.*) to the redirected URL. Remove them from code and
http://mywebsite.com/subfolder1/subfolder2/whatever-path
will be
http://mywebsite.com/subfolder1

htacess redirect from php page to a different website?

This seems so trivial but I can't seem to get it to work. I'm trying to redirect
http://website.com/something/file.php?var=1
to
http://website2.com/something/$1/
I've tried a bunch of things the latest was
Redirect 301 ^file.php\?var=([0-9]+)$ http://website2.com/something/$1/
I've even tried doing
Redirect 301 ^something/$ http://website2.com/something/1/
but that didn't work either. The only thing that I've gotten to work is a redirect on the whole site doing
Redirect 301 /$ http://website2.com/something/1/
Is there something I'm missing? I've done a lot of redirects in my day but this one is throwing me for a loop. I've even used htaccess checker that said my url matches my string but it didn't actually do anything.
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} /something/file\.php\?var=(\d+) [NC]
RewriteRule ^ something/%1? [R=302,L,NE]
RewriteRule ^something/(\d+)/?$ something/file.php?q=$1 [L,QSA,NC]

.htaccess redirect whole site to specific url new site

I have this code in my .htaccess:
Redirect 301 / http://newurl.com/
But when I go to the site example http://url.com/demo, I redirect to http://newurl.com/demo but I want only http://www.newurl.com so everything after the slash (/) has to be cut off.
How can I do this?
Alternative solution using mod_rewrite
RewriteEngine on
RewriteRule (.*)$ http://newurl.com/ [R=301,L]

htaccess redirect a url with a param and remove duplicates

I have this url
http://www.mmametals.com.php5-20.dfw1-1.websitetestlink.com/?page_id=61
and i want to redirect to the root like this
http://www.mmametals.com.php5-20.dfw1-1.websitetestlink.com
here is my htaccess
RewriteEngine on
redirect 301 /?page_id=61/ http://www.mmametals.com.php5-20.dfw1-1.websitetestlink.com
but nothing is happening...any ideas
Also I was wondering if there is a way also in .htaccess to delete a duplicate /about ...so for example if the url is
http://somesite.com/about/about
it will rewrite the rule to always be
http://somesite.com/about
RewriteEngine On
RewriteCond %{QUERY_STRING} ^page_id=61$
RewriteRule (.*) http://www.mmametals.com.php5-20.dfw1-1.websitetestlink.com? [R=301,L]
RewriteCond %(REQUEST_URI) ^/about/about[/]?
RewriteRule (.*) http://somesite.com/about? [R=301,L]
should do it.
I don't know the exact answer off the top of my head as I don't work directly with apache that much any longer, but I think you need to look into apache's mod_rewrite module. Try taking a look at:
Apache's mod_rewrite documentation
This post about blocking certain URLS

Resources