rewrite url using htaccess or hide some text from url - apache

The url showing in the address bar: www.testsite.com/news#tab-1
The url which I want to show: www.testsite.com/news
The url showing in the address bar: www.testsite.com/news#tab-2
The url which I want to show: www.testsite.com/events
I tried rewriting rule using htaccess
RewriteCond %{REQUEST_URI} /news#tab-2$
RewriteRule .* /news[L]
and
RewriteRule www.testsite.com/test www.testsite.com/news#tab-1
But it didnt work. Please help.

You can't rewrite Anchors with .htaccess. You need to use something client side, like javascript in order todo so.
This article i found in another similiar question you can read it here:
Remove fragment in URL with JavaScript w/out causing page reload

Client browsers do not send the character "#" to the server. If you have access to the server Logs you will see all the server gets is "GET /news" and omits the rest. "#" is a client side interpreted character.
You will have to hex encode it in the url if you insist on sending it to the server, but it is probably better if you use a more common URI path or even query string "?" if you want to do internal redirections from the server.
As a friendly side-note. Do not use .htaccess unless you are not the admin of the Apache HTTPD server. It is not necessary to redirect/rewrite as it complicates them and it produces bigger overhead to the server since the file needs to be constantly checked for changes.

Related

Change hostname from the URL in the Apache Web server

I have URL for which i need to change the hostname and keep rest of the parameters as it is in the Apache Web Server. For E.g.
Actual URL
/servereq?hname=cs_load_cs&summary=true&contractID=da588be1a59f47cf8f
To be URL: /servereq?hname=cs_load_cs&summary=true&contractID=da588be1a59f47cf8f
I tried implementing the change using the Rule:
RewriteRule ^http://actualUrl.corp.com/(.*)$ http://newUrl.corp.com/$1
But the URL is parsed only before the '?' and rest of the doesn't appear in the result. I tested the code at online Tester.
Therefore i looking for a way to achieve this without lost of any parameters.
Thanks a lot.
HTTP_HOST is not part of match in RewriteRule pattern, it is automatically added by the apache.
Try :
RewriteRule ^(.*)$ http://newUrl.corp.com/$1 [NC,L,R,QSA]

Remove and block unwanted postback string

Google webmaster page found duplicate content due to the following:
If we take this dynamic search page example.com/armin-music-page-1
google found post back string after "page-1" as shown in example below
example.com/armin-music-page-1$dneix
example.com/armin-music-online-page-1&q=sa=x&ei=-a
example.com/music-dance-club-mix-page-1%balbla
example.com/armin-search-page-1#einx
and many random postback strings
My question, how do i remove or redirect to 404 anything that is generated after "page-1" via apache mod_rewrite .htaccess so google finds clean url only
Thank you in advance!
You can get rid of the stuff after page-1 by redirecting to the URL where that's removed:
RewriteRule ^(.+-page-1)(.+)$ /$1? [L,R=301]
(rule needs to be near the top of the htaccess file)
Or if you want to send to 404:
RewriteRule ^(.+-page-1)(.+)$ - [L,R=404]
But one thing you can't do is deal with requests that look like this:
example.com/armin-search-page-1#einx
because the #einx part of the URL is never sent to the server, so there's no way for the server to match against it. All apache and mod_rewrite sees is /armin-search-page-1.

mod_rewrite to redirect url not working

Cannot seem to get a mod_rewrite to work. We have a domain name that has already been printed here, there and everywhere when the website was Flash. It has a # in its trail /#login.php and we want so that when people put this in it redirects them to /login.php. I have already tried this rule but can't get it to work:
RewriteEngine On
RewriteRule ^/#login.php$ /login.php
I have also checked that the rewrite engine is working by using a redirect to google. Just need the out of date #login.php to go to the new login.php
thanks
The # in the URL (or "fragment") is not sent to the server, it's purely for the client side to point to some part of the page. If you see http://hostname.com/#login.php in your address bar, the only thing the server gets is a request for /. You may need to employ some javascript on the page to look at the browser's address bar to find a fragment and maybe send that to the server as a query string.
Try :
RewriteEngine On
RewriteBase /
RewriteRule ^#login\.php$ /login.php [QSA,L]
Mod_rewrite is enabled ? available ?

How to get real request when using mod_rewrite

I'm wondering how to get the "real" requested URL, when using mod_Rewrite. There are several rewrite rules in my htaccess-file for caching-purposes: First there is a check, if a cache-file is existent. If so, the request will be rewritten to the cache-file. Otherwise the request will be rewritten to a php-script, which creates this cache-file.
But I suspect, the rules doesn't match like I want them to. Is there a possibility to trace the "real" requests to see, which URL was requested by the client and which file is requested in the background?
Thanks in advance.
You may want the %{THE_REQUEST} special variable. The mod_rewrite docs say this:
The full HTTP request line sent by the browser to the server (e.g., "GET /index.html HTTP/1.1"). This does not include any additional headers sent by the browser. This value has not been unescaped (decoded), unlike most other variables below.
So if someone enters http://your-domain/path/file.html into their browser and your webserver rewrites /path/file.html into something entirely different, the %{THE_REQUEST} variable will still be GET /path/file.html HTTP/1.1, or something similar.
As for what the request finally got rewritten to, you can turn on logging for rewrite to see what it is:
RewriteLog /some-path/rewrite.log
RewriteLogLevel 9
This would go in your virtual host config and only be used for debugging purposes. The rewrite.log file will contain details on the rewriting process and what the final URI is.

Configure apache vhost to not treat "?" as a query token

I have snapshotted all the pages on my ajax application, and as per google specification [ http://www.google.com/support/webmasters/bin/answer.py?hl=en&answer=174992 ]i've stored those files in a pattern described as "www.example.com/ajax.html?_escaped_fragment_=mystate" .
In the same directory there is a file named, adapted to this example, ajax.html , which is a static html page. No server side is involved at all.
When navigating to www.example.com/ajax.html?_escaped_fragment_=mystate , which contains html content which is different from that found in ajax.html , ajax.html gets displayed in the browser, using curl, wget and google's "fetch as googlebot" tool.
From my understanding the problem is that ? gets treated by the http server [apache 2] the same way as # gets treated by the browser, ie it's considered as a query param char.
So, how do i instruct this VHOST to behave correctly, and send out the www.example.com/ajax.html?_escaped_fragment_=mystate file instead?
Thanks
You could save all your fragments in /fragments/(mystate) and use the apache module mod_rewrite:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=(.*)$
RewriteRule ^ajax.html$ /fragments/%1
This reads as:
If theres a query string "_escaped_fragment_", take the value, put it in variable %1 and go on. Then, if the url also is 'ajax.html' Rewrite ajax.html to /fragments/%1.