rewrite url without change address - apache

I want to show a page instead of another page, without change the second page url.
I know this is possible with htaccess.
I copied that code from a cms htaccess:
RewriteRule ^event-([0-9]+)\.html$ calendar.php?action=event&eid=$1 [L,QSA]
with that code, we will redirect to calendar.php?......... but I want it redirect (without changing the address in address bar) to another site, for example to http://www.google.com/page......
Is it possible?
Thanks ..

If you want to provide content from another site without changing the address in the address bar of the browser that mean you becomes a proxy.
So check apache documention for proxy configuration (this can be done for specific urls only). Even mod_rewrite can do the proxy things with the [P] tag, mod_rewrite will allow a lot more 'specific url' filtering.
Now the job of a proxy, when he have the response from the distant website and he needs to render it for the HTTP client, is only to change the HTTP headers in the response. So only url in Location tags or such specific headers will be altered. You must known that all the HTML content from the distant website will not be altered (the inner links will be on www.google.com and not on your www.whythehelldoiproxygooglewithmysite.com).
If you want to alter this returned content check mod_proxy_html module, this will add some extra stuff before sending the resonse, to do some more reverse proxy alterations.

Related

How to hide part of a URL from clients using htaccess?

I'm working on a website running on an Apache server. The PHP factory makes and takes URLs that look like this:
site.com/page/view/[id]/[vanity-url]
However, the client wants to hide part of the URL from view, so in the browser it appears as:
site.com/[vanity-url]
The server still needs to see the full URL.
I've tried various RewriteRules after hours of searching, and the closest I seem to have come is:
RewriteRule ^page/view/(.*)/(.*)$ /$2 [R,L]
...but that doesn't seem to be doing anything. I know for sure that my .htacces is working.
What am I doing wrong?
Unfortunately, this isn't possible. Let me explain why:
You state that the server needs to see the full URL. Specifically, this includes the id segment, which I am sure your framework needs (it wouln't need it if the server only needs to see the vanity segment). The code you have provided redirects the full URL to the vanity URL, thus making the full URL invisible to the server for the next request.
So, navigating to /page/view/2/about-us would redirect to /about-us, and the address bar would change to reflect that. This causes a new request to be sent to the server, containing only the /about-us vanity URL.
As a result, you would need to rewrite the vanity URL back to the full URL (without redirecting, so that the /about-us stays in the address bar as-is), but you wouldn't be able to do this, as the vanity URL does not contain the id segment, which seems to be a requirement for the framework to serve the correct response. Keep in mind that Apache cannot guess the ID for that particular vanity URL.

redirect a subdomain to a remote url, preferably via only DNS setting or with httpd.conf, without changing url displayed

For example I wish to redirect list.mydomain.com to http://my.emailingapp.com/lists/
but keeping the name displayed in URL as "list.mydomain.com".
Note that all parameters are to be passed over. e.g. list.mydomain.com/?stuff=a should be the same with http://my.emailingapp.com/lists/?stuff=a
Another note: these domains are on different server.
There are many other similar posts, but all of them does not work exactly as I wanted to.
Adding a CNAME record for list.mydomain.com to my.emailingapp.com using which you can achieve the following.
URL remains list.mydomain.com
The arguments get displayed in URL as list.mydomain.com/?stuff=a
Your requirement of having /lists/ should be implementable by URL rewrite rules.

HTTP: Semantics of breaking webpage information into two pages

I have a webpage whose information had to be split into two separate pages. I want visitors to inform of the fact that there are now two different pages, and thus serve visitors of the old single-page page a custom error page like
(Depending on who you are, ) the page you want is either of these:
*Link to page A*
*Link to page B*
This should be in HTML (to obey the general web site style).
The things that I'm unclear about are semantics and apache configuration
What HTTP Status Code to serve? There is 301 Moved Permanently, but I have more than one alternative to offer.
Is it possible to configure apache to my needs without a hacky LocationMatch or similar? Ideally, I would like to use the normal file serving semantics with the exception that the HTTP Status code should not be 200.
Thanks for the clarifying comment.
In answer to your two questions, I'd say:
Yes, 301 is the correct response to give. This will say that the old page was moved permanently, and will redirect the user to the interstitial page with the links.
You can accomplish this by adding a line to the .htaccess file in the directory that the old single page was once in (assuming you keep the new page there too). Or you can add a line to your main httpd.conf file. The lines would look like this:
.htaccess
Redirect 301 ./old_page.html ./new_interstitial_page.html
httpd.conf
Redirect 301 /path/to/old_page.html /path/to/new_interstitial_page.html
EDIT : Without any more context, this really does sound like a redirect scenario. In HTTP terms, the "resource" that was once available at the old location is now available (albeit in parts) at a new location, so you would "redirect" the client to the new location or to the interstitial page.
But if that doesn't seem right to you, you can try a "410 Gone". This says that the resource no longer exists at all, and instead directs the client to an error page. You can then customize the error page to contain links to Page A and Page B. Your .htaccess would look something like this:
# Enable URL matching and match the old page with "Gone" (G), "Last rule" (L)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^oldPage.html$ - [G,L]
</IfModule>
# Special 410 page with links to Page A and Page B
ErrorDocument 410 /path/to/custom/410-page.html

Understanding difference between redirect and rewrite .htaccess

I'd like to understand the difference between redirecting and rewriting a URL using .htaccess.
So here's an example: Say I have a link like www.abc.com/ index.php?page=product_types&cat=88 (call this the "original" url)
But when the user types in abc.com/shoes (let's call this the "desired" url), they need to see the contents of the above link. To accomplish this, I would do this:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)shoes(.*)$ index.php?page=product_types&cat=88
Nothing wrong with this code and it does the trick. However, if I type in the original url in the address bar, the content comes up, but the url does not change. So it remains as www.abc.com/index.php?page=product_types&cat=88
But what if I wanted the desired url (/shoes) to show up in the address bar if I typed in www.abc.com/ index.php?page=product_types&cat=88? How would this be accomplished using .htaccess? Am I running into a potential loop?
Some of the explanation can be found here: https://stackoverflow.com/a/11711948/851273
The gist is that a rewrite happens solely on the server, the client (browser) is blind to it. The browser sends a request and gets content, it is none the wiser to what happened on the server in order to serve the request.
A redirect is a server response to a request, that tells the client (browser) to submit a new request. The browser asks for a url, this url is what's in the location bar, the server gets that request and responds with a redirect, the browser gets the response and loads the URL in the server's response. The URL in the location bar is now the new URL and the browser sends a request for the new URL.
Simply rewriting internally on the server does absolutely nothing to URLs in the wild. If google or reddit or whatever site has a link to www.abc.com/index.php?page=product_types&cat=88, your internal server rewrite rule does absolutely nothing to that, nor to anyone who clicks on that link, or any client that happens to request that URL for any reason whatsoever. All the rewrite rule does is internally change something that contains shoes to /index.php?page=product_types&cat=88 within the server.
If you want make it so a request is made for the index.php page with all of the query strings, you can tell the client (browser) to redirect to the nicer looking URL. You need to be careful because rewrite rules loop and your redirect will be internally rewritten which will cause a redirect which will be internally rewritten, etc.. causing a loop and will throw a 500 Server Error. So you can match specifically to the request itself:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?page=product_types&cat=88
RewriteRule ^/?index.php$ /shoes [L,R=301]
This should only be used to make it so links in the wild get pointed to the right place. You must ensure that your content is generating the correct links. That means everything on your site is using the /shoes link instead of the /index.php?page=product_types&cat=88 link.

Simple modrewrite, how does a link appear?

When you use modrewrite to rewrite your urls, when does the rewrite occur. Will the user be able to see the url before rewrite, when hovering over the link? When they hover over a link will their browser display the rewritten url or the url before it was modified with modrewrite?
The rewrite is done in the server so the user will never know,
PHP also doesn't know what link its pointing to only the script file
mod_rewrite acts on the server side, meaning that apache rewrites incoming URLs before responding to the request. Any HTML links you add to a page will point to the URL you entered, as mod_rewrite doesn't modify any outgoing data.
The rewrite occurs when Apache has parsed the request - before the PHP interpreter has been started.