.htaccess redirect to # anchor link - .htaccess

Is it possible to redirect a URL to another URL ending in a hash for a page anchor link?
The # part will be hard coded, not picked up by from the browser request.
Here's what I've been trying:
RewriteRule ^birmingham/events.html /#events [R=301,NE,L]
The above just takes me to the homepage without the #events.

The problem was, as #Starkeen said, browser caching. Once that was cleared it all worked.

Related

Redirecting to a different directory with htaccess without changing URL

I would like to add some htaccess so that when a visitor goes to:
domain.com/xxx
They would see the content (without the URL changing) from
domain.com/yyy/zzz
Also any subcontent would show... like visiting
domain.com/xxx/inner-page
would show the content from:
domain.com/yyy/zzz/inner-page
I know that can be done, I just haven't figured out the htaccess code for it yet.
Here's the extra part I'm not sure can be done- sometimes those pages may have full URL links pointing to "/yyy/zzz/inner-page"
I would want anyone going to one of those pages to be redirected (changing the URL) to the equivalent "xxx" url... hopefully without causing a loop of too many redirects issue. Is that possible to do?
You may use this rule in site root .htaccess:
RewriteEngine On
RewriteRule ^xxx(/.*)?$ yyy/zzz$1 [L,NC]

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.

Redirect to a page if URL contains string

My business has been bought-out. As such we are re-branding and changing the URL's of our sites. I have 2 URL's pointing to one web server (the old, original URL and the new URL) and what I would like to do is have script on my pages to see if my user has visited the old URL. If they have I would then like them to be redirected to a particular html page to tell them that the site has changed and that they need to update their favourites. I do not want the redirect to be automated.
Basically any time the old URL is detected I would like them redirected to the redirect page to inform them
IF URL contains 'dls.myOLDwebsite.co.uk' THEN REDIRECT to: 'dls.mywebsite.co.uk/redirect.html'
The reason I would like to do this is because if the user is going to the new, correct address (example; dls.myNEWwebsite.co.uk) then I don't want any redirects or messages.
Can anyone assist?
I believe you're looking for this.
You need to add the following to the .htaccess file of your site
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REMOTE_HOST} dls.myOLDwebsite.co.uk ^
RewriteRule ^/$ http://dls.mywebsite.co.uk/redirect.html [R=301,L]

Going extensionless with redirection

My site has more than 500 dynamic pages and i have make all static (same) pages and redirect old url or new url by using the following rule in htaccess file:
RewriteCond %{QUERY_STRING} ^cid=2&gas=nitrogen$
RewriteRule ^purity\.php$ http://Example.com/gases/nitrogen.php? [R=301,L]
and by using this code for all my pages are now static url's
and now i want to make it http://www.Example.com/gases/nitrogen (without extension) and for this i have uploaded the rule which is :
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^\ ]+)\.php\ [NC]
RewriteRule ^ /%1 [R=301]
This code is actually redirecting my php page to non php page but with 404 error.
Im unable to figure out the problem in this.
Pls help!!
There is a problem with what you are trying to do. You have to understand how rewrites work. You're making your webserver serve up a different page that doesn't exist. So by sending an ugly url to a better SEO'd one, you're saying ok server give me this page instead. If the page itself doesn't exist, then the server spits up a 404 error. Your rewrite should be from the page you want them to go to redirects to your old page name without the [R] parameter so your server knows, don't give this page, give that other page, and don't let them know it's being done by displaying that other url, that's what removing the [R] does. Then to prevent the endless redirect loop, don't redirect the ugly page name to the new one. Instead, simply display the redirected url instead of the ugly one with all those ugly looking parameters. however since you've probably already been indexed and search engines know about the ugly pages, I would recommend not only not linking to those pages anymore and use the new page names on all your pages for things like navigation, and referencing other pages, but I would add the canonical meta tag on your pages telling Google and other search engines what your preferred url is. If you had started out without any search engines knowing your url structure with all those parameters you could have skipped using the canonical meta tag as the search engines wouldn't know your pages by those names if you always used a rewrite.

Does mod_rewrite only translate external requests to internal files and not vice versa?

I think this is a very stupid question so I apologise, as i think i may completely misunderstand mod_rewrite.
Say you have a URL
www.domain.com/products/item.php?id=1234
mod_rewrite can rewrite that to a friendly URL
wwww.domain.com/products/item/1234
(for example)
So, if i type in wwww.domain.com/products/item/1234 this will be rewritten to www.domain.com/products/item.php?id=1234 and that page is served. Fine.
But what if you type in www.domain.com/products/item.php?id=1234 - that page will be served but not rewritten to the friendly URL.
So my question is can you rewrite internal file names automatically? For example, all URLs on my site are currently in the www.domain.com/products/item.php?id=1234 format. When a user clicks this link can this be rewritten to the friendly URL? Or should you always hard code in the friendly URL?
Im sorry if that made little sense! Im getting confused because i want to rewrite non-friendly to friendly URL, but then serve the non friendly URL - so wont that cause an infinite redirect loop?
Mod_rewrite can't really internally rewrite URLs across domains, though it could proxy them (using P option in RewriteRule). Assuming that the domain is the same, you could do something to redirect the client's browser to a friendly URL if the old one is used while internally rewriting the friendly URL back to the old one, but they have to be both the same domain. You do this by looking at the actual request (%{THE_REQUEST}) variable instead of looking at the URI, which changes as they get rewritten internally.
This redirects the browser when the old URLs are used to the friendly URLs
RewriteCond %{THE_REQUEST} ^([A-Z]{3,9})\ /products/item\.php
RewriteCond %{QUERY_STRING} id=([0-9]+)
RewriteRule ^products/item\.php$ /products/item/%1? [R=301,L]
This rewrites internally when a friendly URL is used:
RewriteCond %{THE_REQUEST} ^([A-Z]{3,9})\ /products/item/[0-9]+
RewriteRule ^products/item/([0-9]+) /products/item.php?id=$1 [QSA,L]
Mod_rewrite does not automatically rewrite the "none friendly" urls to the friendly urls. You have to add some rules yourself to do this.
Also Mod_rewrite does not modify the links inside your html, css, or whatever you use. You need to change those yourself.
If a user uses the friendly url, it will never know that it is rewritten. Mod_rewrite is tranparent from the user's point of view. You can add a [R] flag to your rules which makes apache send a redirect to the client. This way the client does see the rewritten url.
Redirecting the unfriendly to the fiendly url, should only be done to help search engines (and to prevent link-rot, but that's more rare). This can be done without a redirect loop, unlike Sergey says.
Try looking around here on SO to find a script that does the redirect from the unfriendly to the fiendly url. Let me know if you can't find it, and I'll help.

Resources