I am using following rule for pages e.g. domain.com/item means get item page and URL should be visible as domain.com/item only.
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?page=$1 [QSA,L]
domain.com/item was working fine until few days back but now it redirects to domain.com/item/?page=item.
When I try to open some other page e.g. domain.com/otherpage it is working fine and it is showing domain.com/otherpage only and not redirecting.
The issue is happening only with item page.
When I am using domain.com/item/ it is not redirecting and working correctly and showing domain.com/item/ (though I will not be using /, this I used for testing purpose only).
How to correct this issue?
Related
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]
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.
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.
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]
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.