.htaccess RewriteRule doesn't work without R flag - .htaccess

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.

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.

substore redirect to subdomain

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.

Redirect PayPal IPN URL

I had my script set up on old.example.com/?paypal_notify=1. There were a few people who signed up to my membership plan while the site was on the old URL. I then moved my site to example.com (no more subdomain) and updated my IPN URL in the PayPal settings.
Unfortunately PayPal is still sending IPNs to the old URL for the members who signed up on the old URL. I tried to redirect the IPN with .htaccess from old.example.com
Redirect /?paypal_notify=1 http://example.com/?paypal_notify=1
This isn't working - I think my redirect rule isn't right. Can someone give me a pointer please? Thanks
Rewrite rules only match the path portion of a URL, which excludes the querystring.
When you want to match based on the querystring, you need to add a RewriteCond directive.
This should do what you want:
RewriteCond %{QUERY_STRING} ^paypal_notify=1$
RewriteRule ^$ http://example.com/?paypal_notify=1 [L,R=302]
When you're happy that it works as needed, replace the R=302 with R=301 to make the redirect permanent.

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 works but the URL isn't changing in the address bar?

I have been pulling my hair trying to figure this out but nothing is working. I have a webpage at mysite.com/test.php I want to do a simple URL rewrite and change it to mysite.com/testRewrite/ The code to do implement this should be:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^testRewrite/$ test.php [NC,L]
For whatever reason it's just not working. When you go to the new URL - mysite.com/testRewrite/ it works.
However, if you type in the original URL mysite.com/test.php it doesn't change the address to the new URL in the browser.
mod_rewrite won't automatically enforce redirecting browsers from rewritten URLs. The rule you have simply says "if someone asks for /testRewrite/, internally change it to /test.php". It does nothing to handle actual requests for /test.php, which is why when you try to access mysite.com/test.php, it gives you /test.php.
Now, the problem with mod_rewrite is if you simply add that rule:
RewriteRule ^test.php$ /testRewrite/ [L,R=301]
which will redirect the browser when it asks for /test.php to /testRewrite/, the first rule will be applied after the browser redirects, and the URI gets rewritten to /test.php, then it goes back through the entire rewrite engine and the above rule gets applied again, thus redirecting the browser, thus the first rule rewrites to /test.php, and the above rule redirects again, etc. etc. You get a redirect loop. You have to add a condition to ensure the browser *actually requested /test.php and that it's not a URI that's been churned through the rewrite engine:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /test\.php [NC]
RewriteRule ^test.php$ /testRewrite/ [L,R=301]
This way, after the 301 redirect happens, this rule won't get applied again because the actual request will be GET /testRewrite/ HTTP/1.1, and the condition won't be met.

Resources