substore redirect to subdomain - .htaccess

I have a URL like below
http://www.abcxyz.com/storename
and I have sub-domain with
http://subdomain.adbxyz.com
So I want to rewrite all URLs of http://www.abcxyz.com/storename with http://subdomain.adbxyz.com/
How can I do this using HTACCESS?

If you're using mod_alias and want to do just a permanent redirect then you can add the following line to your .htaccess:
Redirect 301 /www.abcxyz.com/storename http://subdomain.adbxyz.com/

You can't internally rewrite from one host (www.abcxyz.com) to another (subdomain.abcxyz.com) without using a reverse proxy. "Rewrite" means to change the URI internally on the server (or "behind the scenes") such that the browser or client doesn't know about it, the URL in the browser's address bar remains unchanged. To do this, using mod_rewrite in conjunction with mod_proxy:
RewriteEngine On
RewriteRule ^/?storename(.*)$ http://subdomain.adbxyz.com/$1 [L,P]
If what you mean is a "redirect", as in the request is made, and the response is to tell the browser or client to go to a different place, thus changing the browser's URL address bar, then that's simpler:
RewriteEngine On
RewriteRule ^/?storename(.*)$ http://subdomain.adbxyz.com/$1 [L,R]
This would go in the document root of the www.abcxyz.com vhost/server.

Related

301 redirect Dynamic URL into Static URL

How to convert a 301 redirect dynamic url to a static url?
In this case
www.example.com/book.php?book=title
into
www.example.com/book/title
When Requesting the Dynamic URL it should Convert as a static URL.
The dynamic URL should be converted and the static URL should be displayed in the address bar instead.
I got this code :
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^user/([^-]+)/?$ /user.php?id=$1 [NC,L]
Can some one give me a 301 redirect of dynamic url tio static url.
Thanks in Advance.
The easiest way is using Apache's mod_rewrite in your .htaccess file. To get the 'static' (seo/fancy) url in the address bar, you'll need an external redirect. To get the server to do something sensible with it, you'll need an internal rewrite. To prevent an infinite loop, you'll need to use the %{THE_REQUEST} trick.
%External redirect
RewiteCond %{THE_REQUEST} ^(GET|HEAD)\ /book\.php\?book=(.*)\ HTTP
RewriteRule ^ /book/%1? [R,L]
%Internal rewrite
RewriteRule ^book/(.*)$ /book.php?book=$1 [L]
The above rules are untested (but they should work). If they work, change [R,L] to [R=301,L]. This will make the redirect permanent. This will cache the redirect in the browser and lessen the amounts of requests to your server and make search engines 'remember' the right url. You don't want to do this while testing though, because it might show a previous attempt (that is cached) instead of sending the request actually to the server.

htaccess won't rewrite url

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^product/([0-9]+)/([0-9]+)/([0-9]+)/([0-9]+)/([a-zA-Z]+)/$ product.php?id=$1&srl=$2&item=$3&page=$4&title=$5#titleProduct
When I hit f5 button for refresh, the URL remains and it's not rewrited. I tried to access with the "new" rewrited link, but it's not working.
Wow, that means I'm need to update every single file for the "new" rewrite? All files contains different hyperlinks with the "old" link. I thought if I write a .htaccess file, all url will automatically rewrite.
You're thinking of a browser redirect, that changes the address bar, a rewrite takes the nicer looking URL and internally rewrites the URI to something the server can understand. See the top half of this answer for a better explanation of this process.
So you can do a browser redirect if the browser actually requests the product.php file, then redirect to the fake nice looking URL. The browser will then resend a new request, for the nice looking URL and the server gets that, internally rewrites it back to the php file (the rule that you have).
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /product\.php\?id=([^&]+)&srl=([^&]+)&item=([^&]+)&page=([^&]+)&title=([^&\ ]+)
RewriteRule ^product.php$ /product/%1/%2/%3/%4/%5/ [L,R=301]
This will take a /product.php\?id=123&srl=abc&item=qwerty&page=blah&title=something URI and redirect the browser with a 301 to the nicer looking URL. Then you're rule should internally rewrite it back.
Regardless, you really should change the links you serve to the nicer URLs, relying on mod_rewrite to do both ends of the work is really inefficient.

.htaccess RewriteRule doesn't work without R flag

I have this rule :
RewriteRule ^questions/([a-z]{2})/(.*)$ $1/$2 [L]
But this rule only works when I add a R=301 flag... The problem is that the url is rewritten in the address bar of the browser.
How can I redirect and keep the url that the user typed in the address bar ?
A 301 is sent back to the browser, which sends a new request to the redirected page. That is why the browser is redirected.
Use the Local redirect.
In order to keep the user url, you should use the "ProxyRequest" in your vhost.conf.
mod_proxy.

HtaccessRules to redirect my website

Hi can anyone of you suggest me on how to redirect my page http://testsite.com/about.php to http://testsite.com/about/ by using htaccess rewrite rules.Without using any query string in the page url i need to rewrite it
I don't think you want a redirect but a rewrite. A redirect means informing the client to requery for the redirected url, whereas rewriting means telling the server to interpret an url as being some other url.
and you dont' want to rewrite about.php to /about/ but the other way around. You want your users to type in /about/ and that url to be handled by /about.php
and this is done by:
RewriteEngine on # this line enables rewrite
RewriteRule /about/$ /about.php
RewriteRule /about$ /about.php
I'm not sure if both of the above are actually needed or just one (I don't have access to an apache right now to test)
In the slight chance that you actually do want a redirect as you wrote, use this free redirect generator: http://www.htaccessredirect.net/ (it's easier than learning all the quirks of the conditions to match up)
in your case that will be:
Redirect 301 /about.php /about/

301 redirect urls

I'm trying to redirect an old url to a new one using 301
I need an example of RewriteQueryString for the following 301? http://www .example.com/search/?depId=1&typeCatId=1 to the following http://www.example.com/mens/clothing
So when I type in the long URL in the browser, I am redirected to the new, shorter URL
Any ideas?
RewriteEngine On
RewriteRule ^search/\?depId=1&typeCatId=1$ /mens/clothing [R=301]
^ Try that.
You could use mod_rewrite either in your .htaccess file or the apache configuration. You might take a look at the RewriteMap feature if you are going to have a lot of different departments, etc. to map. Using the [R] flag after the RewriteRule will cause the browser to redirect instead of just being an internal redirect. Using [R=301] will make it a 301 redirect.

Resources