HtaccessRules to redirect my website - .htaccess

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/

Related

HTACCESS: Rewrite a Previously Rewritten URL Causes Error

not sure what my problem is, but I insert one rewrite into my htaccess file and the whole thing breaks. I used an htaccess tester and it says the rule works fine.
For some reason Google indexed the php file that I normally rewrite URLs to. So I need to take the .php page that displays and send it to the proper rewritten URL.
Here is the rule:
RewriteRule ^products/Automotive-Transmission-Torque-Converters-results.php$ /Search-Results [QSA,R=301,L]
The only thing I can think that would be causing the problem, is that I use this URL in another rewrite later on in my code.
Here is the second rewrite later on in the file:
RewriteRule ^Search-Results/?$ /products/Automotive-Transmission-Torque-Converters-results.php [QSA]
I hope I've identified the problem correctly. But again, I put the first rewrite rule into my htaccess and the the regular page doesn't show, the browser says there is an error on the page. Yet, htaccess testers say the rule works fine. Hence my suspicion.
RewriteRule ^products/Automotive-Transmission-Torque-Converters-results.php$ /Search-Results [QSA,R=301,L]
RewriteRule ^Search-Results/?$ /products/Automotive-Transmission-Torque-Converters-results.php [QSA]
Yes, these two directives used together will result in a redirect-loop (something that online testers don't appear to check for). The problem is that the first rule will trigger an external redirect after the second rule has rewritten the URL (when the rewrite engine starts over - in .htaccess).
The solution is to make sure the first rule (the external redirect) only triggers on direct requests from the client and not requests that have been internally rewritten.
We can check for direct HTTP requests by checking against the REDIRECT_STATUS environment variable, which is empty on the initial request and set to "200" (as in 200 OK HTTP status) after the first successful rewrite.
For example:
# Redirect direct request for PHP file to canonical URL
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^products/Automotive-Transmission-Torque-Converters-results\.php$ /Search-Results [R=301,L]
# Internally rewrite the canonical URL
RewriteRule ^Search-Results/?$ /products/Automotive-Transmission-Torque-Converters-results.php [L]
Note that I have removed the QSA from both rules, since it's not required here (the query string is appended by default). And I've included the L flag on the second rule. Also, don't forget to backslash escape the literal dot in the RewriteRule pattern.
Test first with 302 (temporary) redirect to avoid potential caching issues and make sure your browser cached is cleared before testing.
For some reason Google indexed the php file that I normally rewrite URLs to.
However, you need to try and identify how Google managed to find the PHP file/URL in the first place. If this is the a result of an incorrect internal link on your site then it needs to be fixed.

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.

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.

How to pass URL Parameters through a .htaccess file

I need to transparently pass a URL parameter through a .htaccess 301 redirection but I am unfamiliar with how to code it.
For example, an Adwords clickthrough appends the following parameter to the landing page url:
&gclid=CKCPq62Sq6wCFY1S4god4FZd1g
Our Google landing page is being redirected like this:
Redirect 301 /old-page /new-page
(We don't want to edit our Google Ads as doing so would lose our existing stats. Thus the redirect..)
How do I preserve the above gclid parameter while redirecting in .htaccess?
Thanks,
Geoff
You can't handle dynamic query strings properly with standard htaccess 301 redirect. In order to do so you should use mod_rewrite:
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule /old-page /new-page?%{QUERY_STRING} [R=301,L]
See, http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html
NOTE: I'm not positive that RewriteCond is required, but it's there to prevent the page from redirecting over and over again. Depending on the rest of the layout of your site, you may need it... although in most cases it's not needed unless you're redirecting all requests.

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