Apache .htaccess redirect to an anchor - .htaccess

I'm trying to do a one-off damage-limitation redirection to an anchor on a page on a website. A wrong URL got published in some publicity material, like this:
https://mydomain.org.uk/A/B
when what I really wanted to publish was
https://mydomain.org.uk/A#B
Having looked at some other answers it seems that any redirect with an anchor needs to be an absolute URL. So I put this in my .htaccess:
RewriteRule A/B https://mydomain.org.uk/A.php#B [NE,L]
(note, the .php is correct, A.php is the page file). And it just simply doesn't work. The browser simply loads A.php and displays it from the top.
I know that the rule pattern is matching, because if I make the target be a completely nonexistent page I get a 404 as expected.
Unfortunately my web hosting service doesn't let me use the Apache log, so it's hard to trace what's going wrong. Can anyone guide me to how to do the rewrite properly so that I pass the #anchor all the way through to the user's browser?
Thanks in advance!

When the RewriteRule is processed by the server, it basically changes internally which resource to access, without the browser noticing.
The only way to change the URL in the browser is to use the redirect flag. This will make the webserver send a HTTP 302 response with a Location header, which then will result in the browser changing the URL and requesting the new page. This new URL can contain an anchor.
In your case the following rule should work:
RewriteRule A/B https://mydomain.org.uk/A.php#B [NE,R,L]
Please keep in mind that anchors are a browser feature so they are normally not sent to the server and therefore neither appear in access logs nor can be used in a RewriteRule.

Related

Will an redirect setup in IIS 6 work for a form POST?

I have a website where a third party performs an HTTP post to a file - let's call it http://mywebsite/third-party-post.cfm (it's a ColdFusion website).
I want to move the code to a new file - let's call it http://mywebsite/third-party-post-new.cfm.
If I change the file attributes in IIS:
Will this pass the form fields for a POST action, or does a redirect like this work only for GET?
You cannot pass post parameters during the redirect. The web server actually makes a call back to the browser to ask it if it can move the page, the browser then resubmits a request to the URL the server is asking to move it to. This is I believe a 301 call to the browser.You never see this and it happens in milliseconds. When the redirect happens it will destroy any post parameters that were submitted in favor of the new request coming from your browser, the server will see it as a brand new request with no post data.
You will need to pass your data in some other way to the page, either through a GET request or a include of the script you wish to run on the old page.
you might be better off actually using a URL rewrite to change how the web server sees a request for that given page.
the apache script would look something like this
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^third-party-post.cfm /third-party-post-new.cfm [L,QSA]
The above code will take a request for the orignal page and translate it to a request for the new page. If will still maintain the original URL though but will display the new page.
IIS has a URLrewrite function you can utilize or if you are familiar with Htaccess and apache, you can use a Helicon Ape http://www.helicontech.com/ape/ to read htaccess files for your site.
See post https://stackoverflow.com/a/13628908/2482184. Although this question is about apache the same is true for IIS no matter which version you are using. There are many other options available to bypass this issue. One of them is to create the /third-party-post.cfm file and include the /third-party-post-new.cfm. You can even use a new HTML5 technique to change the url without refreshing the page (see http://spoiledmilk.com/blog/html5-changing-the-browser-url-without-refreshing-page/ ).

What to do with dynamic url after rewrite?

After you've been able to successfully create a url rewrite how do you handle the original and other possibly ways to access a page. This of course to prevent duplicate content. For example if I have this:
RewriteEngine on
RewriteBase /
RewriteRule ^blog/(\d+)/([\w\-/\.]+)/?$ blog.php?id=$1&article_title=$2 [L]
I'm able to access the page by the url
https://www.mysite.com/blog/10/mysite.com (the mysite.com is the article title)
The problem is I'm also able to access the site by going to
https://www.mysite.com/blog.php?id=10article_title=sitetitle
https://www.mysite.com/blog.php?id=10
ect.
How are you supposed to handle those particular urls.
Also should I change the blog.php?id=10 to the rewritten url? Can I rely on something else and just start using the full rewritten url now? The site is new.
For my web site, I have the script that gets called from inside the rewrite detect the URI they were fetched from (using the "REQUEST_URI" variable that at least Apache sets), and redirect to the canonical one if they ever get called with the internal one (outputting a 301 direction).

redirecting url and showing the short url on the adress bar

I've seen a lot of sites doing this but haven't found a guide to explain this.
I have this .htaccess code
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^users/(\d+)*$ ./profile.php?id=$1
It redirects site.com/user/1 to site.com/profile.php?id=1
but when it redirects I want the user to see in his adress bar the shortened url (site.com/user/1)
How can I do this?
Rewrite is internal logic of the server, i.e. the browser requested for the long URL you see in the address bar, and the HTTP server routed it to the shorter one. If you want the browser to show the shorter URL, you will need to use Redirect instead of Rewrite.
Note that with redirect, the page will be slower to load, as the server returns the redirection response to the browser, which then requests the page once again.
There are plenty of sources on how to do this. This is the first one I got when I googled it.
http://net.tutsplus.com/tutorials/other/using-htaccess-files-for-pretty-urls/
A rule works like:
RewriteRule <pattern_A> <target_B>
It always works from <pattern_A> to <target_B>, never the other way around, there's no "linking" of the 2 requests by this rule. The top part of this answer addresses the 2 different things that happens when "URLs get changed for another URL".
The rule that you have takes a URL without a query string and internally rewrites it to a php file with a query string. The browser doesn't see this happen, it's "behind the scenes". When you say you want the address bar to show the URL that doesn't have the query string, you'd first be going to some URL that's something else, and that "something else" URL needs to externally redirect the browser. So under the rules that you already have:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /profile\.php\?id=([^&\ ]+)
RewriteRule ^ /users/%1? [L,R=301]
That matches against a request string, not the request URI (which gets changed along the URL processing pipeline and the rewrite engine), extracts the ID, and redirects the browser to the new URL without the query string by using the R=301 flag.
You really need to change all of your content so that they use the cleaner looking URLs instead of relying on a very cumbersome and inefficient way of redirecting the browser, then internally rewriting back to the original URL.

htaccess rewriterule index.html

I'm using a PHP framework which redirect all URL to the index.php file using the following rule:
RewriteRule ^(.*)$ /index.php/$1 [L]
However, I want to be able to use index.html IF ANY ONLY IF users hit the home page. For instance, if users hit http:// website.com it will render the index.html file. Any other URL will use index.php. Could someone help?
ThankS
^/(.+)$
Matches everything but the root.
However, either way this rewrite strategy is questionable. Sending absolutely everything to PHP — including robots.txt, favicon.ico, crossdomain.xml, other random things a bot might speculatively request, and all the automated break-in attempts on odd paths that don't exist — is likely to hit performance hard.
(It's also a potential problem if the script doesn't reply 404 properly for non-existant paths: someone could use file-based domain authentication to take over Google Webmaster Tools for your account.)
I'd try to put some limitations on what kind of paths are allowed to match.

problems getting nice browser url using redirect/rewriterule

Currently I use a .htaccess redirect to send a (nice) url /offices/london/whatever to my script (nasty url) /db/db.pl?offices-london-whatever
i want the browser url to be nice, with the 301 redirect it isn't so i tried with the RewriteRule but the browser url is still the nasty one.
e.g. RewriteRule Offices/London/(.*)$ /db/db.pl?Offices-London-$1 [NC]
it all navigates, i get the pages i want with either method, but i want the nice url not the nasty one for both the user browser and SEO. presently i only get the nasty url
any clues what i am doing wrong?
Let's assume the following:
RewriteRule ^/Offices/London/(.*)$ /db/db.pl?Offices-London-$1 [L,NC]
This makes your page accessible through www.yourdomain.com/offices/london. So you can just use that URL in your browser. As for SEO the crawlers will see you are using that URL in your links and will index it.
Remember that you can always use the other URL (the nasty one) aswell, just dont use it (except for testing ofcourse).
ok, thanks for that
the problem is not one of 'accessing' the script, that all works fine, it is of getting the browser address bar to NOT display the ugly path/url, which happens with the example above.
as for the SEO, it is not the case, google is currently displaying the ugly url.
by reading through http://www.webmasterworld.com/forum92/6079.htm (and www.askapache.com/htaccess/mod_rewrite-basic-examples.html) i am slowly getting there, with two rewrites and a cond, but i have been lazy in my perl and the relative paths are screwed, so got to do some more on it.
for now though, i got to do some other pesky customer things for a while.
will post my full solution here shortly!!!

Resources