Redirect from one address to another - .htaccess

I want to redirect $weird_address to $home_address, for example:
$weird_address = "https://homepage.com/?val=search&bname=&town=Yanziyan&country=China&ll=on&full=full&ss=1"
$home_address = "https://homepage.com/"
I was trying a lot of different posibilities:
1. RewriteRule "https://homepage.com/?val=search&bname=&town=Yanziyan&country=China&ll=on&full=full&ss=1"
"https://homepage.com/" [R=301,L]
2. RewriteRule "^/\?val=search\&bname=\&town=Yanziyan\&country=China\&ll=on\&full=full\&ss=1$"
"https://homepage.com/" [R=301,L]
3. RewriteRule "^(.*)China(.*)$"
"https://homepage.com/" [R=301,L]
4. Redirect 301 "https://homepage.com/?val=search&bname=&town=Yanziyan&country=China&ll=on&full=full&ss=1"
"https://homepage.com/" "https://homepage.com/"
And much more.. But nothing has worked. My question is how to redirect that link? I've done research on the internet but I haven't found nothing.

It's hard to tell what exactly it is your want to, but the syntax is like:
Redirect /path/to/old.html http://www.example/path/to/new.html
Redirect /path/to/old.html /path/to/new.html
Do you get an error message?

Related

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]

Rewriting Dynamic URLs in htaccess not working

I want to rewrite
mypage.com/country/country.php?country=something
to
mypage.com/country/something
in the address bar, using htaccess
I've tried many things and looked everywhere and the closest I've got is:
RewriteCond %{QUERY_STRING} country=([^\&]*)
RewriteRule ^country.php$ /country/%1? [R,L]
But this just produces a rewrite loop that alternates between the two links above and I don't understand why.
I want both
mypage.com/country.php?country=something
and
mypage.com/country/something
when entered, to show
mypage.com/country/something
in the address bar
Any help?
If you have a rewrite loop it suggests that besides that rule, you also have a rule that translates it back. You'll need the 'ugly' url to only trigger when it is an external request. The easiest way to do that is by matching %{THE_REQUEST}.
#Ugly to fancy url; should be R=301 when it works
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /country\.php\?country=([^\&]*)\ HTTP
RewriteRule ^country.php$ /country/%2? [R,L]
RewriteRule ^country/(.*)/?$ /country.php?country=$1 [L]

General rulefor redirect in htaccess

I want to make some redirects in htaccess. I will explain in detail what I'm trying to do and at the end I will tell you my question, so if you don't want lots of explains just scroll down.
So for you to understand what I am doing, here is an example: if I enter www.mysite.com/oldpage it should redirect me to www.mysite.com/new page. For this I used:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^mysite.com [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [L,R=301]
Redirect /oldpages http://www.mysite.com/newpages
The problem is that I want to use this for more than one page. So I want that:
mysite.com/oldpage/firstarticle
to redirect to
mysite.com/newpage/firstarticle
And the same with all articles. I want to redirect all like:
Redirect /oldpages http://www.mysite.com/newpages
Redirect /oldpages/firstarticle http://www.mysite.com/newpage/firstarticle
Redirect /oldpages/secondarticle http://www.mysite.com/newpage/secondarticle
But I have a conflict because Redirect /oldpage works on every case.
That's why (AND HERE IS WHAT I DON'T HOW TO DO!!!) I need to use a general rule like:
Redirect /oldpage/(*) http://mysite.com/newpage/(*)
Redirect /oldpage/(any numeric characters-don't need them)/(*) http://mysite.com/NEWSINGLEPAGE/(*)
Instead of (*) it should be any characters,but use them in the new link.
Does someone know how to make this Redirect for more than one page?
Thank you for your time!
!!!!
I DON'T KNOW IF IT'S POSSIBLE BUT IT WOULD BE NICE IF I COULD USE AN IF STATEMENT IN HTACCESS...SOMETHING LIKE:
if (adress is /redirect) Redirect to newpage.html;
else if (adress is /redirect/(some number)/(.*)) Redirect to NEWSINGLEPAGE/$1.html
else if (adress is /redirect/(.*)) Redirect to newpage/$1.html
Change
Redirect /oldpage/(*) http://mysite.com/newpage/(*)
To
RedirectMatch /oldpage/(*) http://mysite.com/newpage/$1
try this
RewriteRule ^oldpage/(.*)$ /newpage/$1 [R=301,L]

mod_rewrite so that first-level subdirectory is a GET variable

Alright, title is REALLY sloppy.
Here's my problem: I have a news site and when you go to the main page (domain.com) it redirects you to domain.com/news/top?geography=San_Francisco after it figures out your geography.
How do I use the .htaccess so that it goes from domain.com/news/top?geography=San_Francisco domain.com/San_Francisco/news/top ?
There are some similar questions, but I have not found one similar enough in that you're editing the URL as a furtherback subdirectory.
It should also be noted that I am using the Code Igniter framework for PHP and it normally has it as domain.com/index.php/news/top?geography=San_Francisco but I did a mod_rewrite already to get rid of the index.php. The code is as follows for that:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Code I've tried:
RewriteRule ^([^/]+)/news/top$ /news/top?geography=$1 [L,QSA]
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Before the index.php rule that you have, try adding this:
RewriteRule ^([^/]+)/news/top$ /news/top?geography=$1 [L,QSA]
You'll need to make sure the links you generate are in the form of domain.com/San_Francisco/news/top though.
But to take care of the links in the wild that still look like the old way, you have to match against the actual request:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /news/top\?geography=([^&]+)
RewriteRule ^news/top$ /%1/news/top? [L,R=301]
This will 301 redirect the browser if someone goes to the link domain.com/news/top?geography=San_Francisco and make it so the browser's address bar says this: domain.com/San_Francisco/news/top. At which point the browser will send another request for the second URL, and you use the rule above to change it back into the one with a query string.

Question about a redirect

I just noticed that sometimes (even when given a wrong url) load perfectly fine. How do they accomplish this? What I mean is, suppose you click on a link that seems good like www.foo.com but it contains in the end a space character which would appear on the address bar as www.foo.com%20 some sites manage to redirect this to their correct url while others just break. How can this be achieved? I'm guessing it's something to do with the .htaccess but I have no idea what to do or where to do it.
The URL I'd like to redirect looks like this actually: http://foo.com/%C2%A0
I get the following error message:
The requested URL /%C2%A0 was not found on this server.
How can I make this redirection?
So far I came up with:
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^%?\ ]*\%
RewriteCond %{REQUEST_URI} !^/
RewriteRule ^(.*)$ http://www.foo.com/ [R=301,L]
but it's not working at all

Resources