htaccess won't rewrite url - .htaccess

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.

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.

Redirect all URLs starting with https:// to same URLs starting with http://

I've been looking for this solution but can't get it anywhere.
I have a website (shop) with SSL set up on it, working fine except in the area where customers would get an URL to the file they just purchased.
So, my working url to a download file should look something like this:
https://www.mywebsite.com/index.php?eddfile=123456etc
But the files only work if you browse them without HTTPS prefix:
http://www.mywebsite.com/index.php?eddfile=123456etc
So what I need is just to remove the https from these URLs that start with:
https://www.mywebsite.com/index.php?eddfile
And redirect them to the same URLs but without https prefix rather with regular prefix:
http://www.mywebsite.com/index.php?eddfile
Note: this is not a regular https to http (or vice versa) redirection for which I found answers here - Although this should be a simple .htaccess redirection, I need to make sure that this only happens to the urls that are beginning as described above
I tried to do something like this:
RewriteEngine On
RewriteCond %{REQUEST_URI} eddfile
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]]
But no success with that
As Martin suggests in comments, the real solution would be to fix why your HTTPS URL does "not work". (But also, why not just link directly to the HTTP URL if that "works"?)
Anyway, to answer your specific question... try the following near the top of your .htaccess file instead:
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteCond %{QUERY_STRING} ^eddfile=.
RewriteRule ^index\.php$ http://%{HTTP_HOST}%{REQUEST_URI} [R,L]
This is only a "temporary" (302) redirect, since this is only a "temporary" fix.
eddfile is part of the query string, not the URL-path. The query string is automatically passed through to the substitution, providing you don't provide a query string.
Since you say the HTTP URL "works" then I assume you don't have an HTTP to HTTPS redirect?? Otherwise, you would need to include an exception with this redirect in order to avoid a redirect loop.

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.

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/

Resources