I want /rss-2/ to be reached at /rss/news.php (this file does not exist) /rss-2/ currently shows the feed (generated by a wp plugin), I want to show it on the other page as well.
I tried
RewriteCond %{REQUEST_URI} /rss/news.php
RewriteRule ^(.*)$ /rss-2/ [R]
but I couldn't find an appropriate flag that renders the destination instead of redirecting to it.
Wordpress looks at the url in the address bar, not how you rewrite the url. You can use the [P] flag to proxy the request, but I heard this is quite expensive since you are doing an additional request to your own server with that url. The second request will see rss-2 and will handle it accordingly, which will pass it on to request 1 which will show it to the user. Additionally, the proxy connection has to be set up for each request.
See this page for more information.
Related
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.
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).
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 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.
i need to hide full path and show shortly:
www.site.com/this/is/path/to/hide/page.php --> www.site.com
Any idea to do it with .htaccess apache and rewrite rules??
EXAMPLE:
If i type www.site.com i want open index.php (in /),
but if i go to /hidden/path i want to open file.php (in hidden/path)
mantaining browser url in www.site.com.
EDIT:
i want to see in the browser bar www.site.com and i want to open page at /this/is/path/to/hide/page.php .
thanks
As I explained in : How does url rewrite works? every rewrite triggers a new call to the rewritten URL. (HTTP 3xx code).
So the client will ask for www.site.com/this/is/path/to/hide/page.php, would be redirected to www.site.com and will be served the index page as a normal user.
There is no way to tell the client to display one URL in the browser bar instead of another, client browser will always make a new request. (Or you could impersonate any site for example)
If you are not against the use of a cookie, or can use environment variable you may be able to do something like :
RewriteRule this/is/path/to/hide/page.php / [co:knowHiddenPath=true]
The environment variable as same syntax with E instead of co.
(See http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html for cookie information)
Your index page should then check this cookie/variable to serve the hidden page or not.
Another solution would be to enable access with password to your file. So even if someone know the URL he would not access the file. Security by obscurity is does not exists.
You can I believe do that with an Alias,
Alias / /this/is/path/to/hide/page.php
This directive needs to be in your <VirtualHost>
This will use mod_rewrite and you can put this into your .htaccess
# Activate Rewrite Engine
RewriteEngine On
# Home page rewrite rule
RewriteRule ^$ /this/is/path/to/hide/page.php [QSA,L]
This will ONLY work if you hit website root (e.g. http://www.example.com/)