URL Rewrite all calls to external URLs - php

I've written a simple PHP script on shared hosting and wish to implement some rules in the .htaccess file so that every time my script calls, let's say, http://www.google.com/test1 it will get http://www.otherwebsite.com/test1 instead.
I've used standard URL Rewriting Rules before but didn't need this specific function.
Thank you !

If I understand your question correctly you want to redirect all of your URLs to some other domain.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?yourwebsite\.com$ [NC]
RewriteRule ^ http://www.otherwebsite.com%{REQUEST_URI} [R=301,L]

.htaccess can only rewrite incoming URLs with domain names that resolve to your web server. It has no control over outgoing URLs as these requests are directly going to outgoing web servers (in your example, google.com).
What you probably need is a scripting solution that redirects the user as you desire by hooking in to onclick events of all the external links. Here's a quick proof of concept with jQuery but this can be done using standard JavaScript as well.
<html>
<head>
<title>jQuery global redirector</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
</script>
</head>
<body>
google url would redirect<br />
mysite.com url won't redirect
<script>
<!--
$(function() {
$("a").click(function(e) {
var url = e.target.href;
if(!(url.startsWith("mysite.com") ||
url.startsWith("http://mysite.com"))) {
var path = $( '<a />', {href : url} ).prop( 'pathname' );
window.location.href = "http://otherwebsite.com" + path;
e.preventDefault();
}
});
});
//-->
</script>
</body>
</html>
You should probably put the script inside another file (say redirect.js) and then include this script selectively in your pages that need such redirection. And don't forget to import jQuery as well.

Related

Url friendly without losing styles css / js / images

I have a website too extensive editing the URLs of styles css and files js, images would cost me too much time.
I'm working at localhost
localhost/project/index.php
The friendly url I want to have:
localhost/project/index/
From this url
localhost/project/online.php
to this
localhost/project/online/video/hd/free/
I have observed this website http://crazycafe.net/demos/seo/ and its source code, the styles css is maintained this way: <link href="css/bootstrap.min.css" rel="stylesheet" type="text/css">
without adding an absolute URL to styles css and their files and images.
How do i create a friendly url this way?
If the user modifies the url in this way:
http://crazycafe.net/demos/seo
Redirect to this way:
http://crazycafe.net/demos/seo/
On the other hand like conserving the styles css the files js and images, without having to modify to an absolute route.
My file directory is:
assets/css/style.css
assets/js/app.js
assets/fonts/icons/image.png
assets/fonts/ttf/roboto.ttf
assets/img/system/image.png
assets/img/logo.png
Note:
I found this question in SO, of how conserve CSS styles.
But I do not understand the use of friendly URL - .httaccess
You need to switch on mod_rewrite and use it. Example:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^folder1.*$ http://example.com/ [R=301,L]
Folder-based example:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^folder1/(.*)$ http://gs.mt-example.com/folder2/$1 [R=301,L]
Your case:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^online.php$ project/online/video/hd/free/$1 [R=301,L]
See more here.

Page resources fail to load when removing '/' slash from URL upon .htaccess redirect

I have a simple redirect rule as follows:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^contact-us\/?.*$ ./contact-us.php [NC,QSA,L]
This rule works fine for the following URL:
http://localhost/folder1/folder2/folder3/contact-us/
The problem is that if I remove the final slash, the page loads properly but the resources (css/js) are not loading
http://localhost/folder1/folder2/folder3/contact-us ----> This fails
It seems it is omitting a folder when trying to load the resources so rather than http://localhost/folder1/folder2/folder3/js/jquery.js, the URL for the JS resource is being set as http://localhost/folder1/folder2/js/jquery.js
Is there a concept missing here with the rules? How to make both URLs work?
This is happening due to your use of relative paths in js/css etc.
To fix, you can add this just below <head> section of your page's HTML:
<base href="/folder1/folder2/folder3/" />
so that every relative URL is resolved from that base URL and not from the current page's URL.

How do I rewrite the URL for example.com/index.php#anchor to example.com/anchor?

I have a page (example.com) with anchors at major sections (example.com/index.php#anchor, example.com/index.php#about, example.com/index.php#contact, etc.) as well as subpages (example.com/subpage.php).
Here's my .htaccess file:
RewriteEngine On
DirectoryIndex index.php
ErrorDocument 404 http://example.com/my404page.php
RewriteRule ^anchor/?$ index.php#anchor [R,NE,L]
RewriteRule ^subpage/?$ subpage.php [NE,L]
The subpage rewrite rule works great.
For the anchor link, including the "R" (redirect) flag changes the URL to example.com/index.php#anchor, but I want the URL to be example.com/anchor.
If I remove the R flag and enter example.com/anchor into my browser, than it goes to the index page and keeps the clean URL (example.com/anchor) but doesn't scroll to the anchor.
So... Is there any way to achieve this in .htaccess, or is there a good way to achieve this with JavaScript?
It's not possible to do that with URL rewrite. Because the browser scroll down when the # is part of the URL... not on the server side.
You can't do this with .htaccess. But you can do it with the following JavaScript code:
window.location = String.prototype.replace(window.location, /index\.php#(.*)/, function(match, g1){return g1;});

.htacces Appears only html with no CSS or PHP

I've been wondering on the internet on how to work with htacces(really hard to learn it). And when I was lurking in the internet, I found this: http://www.generateit.net/mod-rewrite/
Well, I inserted my url(working on localhost):empresa.com/index.php?p=sub_artigo&id=1&cat=Mercearia
and it gave me this(with all options by default):http://empresa.com/sub_artigo/5/Mercearia.html
And the .htacces code was this:RewriteRule ^([^/]*)/([^/]*)/([^/]*)\.html$ /index.php?p=$1&id=$2&cat=$3 [L]
And when I generate the url in php I do
$response2[nome_sub_artigo]
And then, when I click the button, it appears like, only html.
example: http://s14.postimg.org/wr137fx4x/htacces_error.jpg
Any idea what is happening ?
It looks like you are using relative links for your assets (images, javascript, css).
That means that when you look for css/my_stylesheet.css, from the new url, the browser will request a url like http://empresa.com/sub_artigo/5/css/my_stylesheet.css.
The easiest solution is to always use absolute urls for your assets, like /css/my_stylesheet.css, etc.
Your PHP code is outputting relative paths to the style sheets.
Example:
<link rel="stylesheet" type="text/css" href="styles.css">
Given your example input URL, this causes the browser to look for a stylesheet at the following URL:
http://empresa.com/sub_artigo/5/styles.css
This is because the browser doesn't know that the URL has been rewritten - it believes it is viewing a file in a subdirectory that doesn't really exist.
Instead you should use an absolute path, such as:
<link rel="stylesheet" type="text/css" href="/styles.css">
Notice the leading / on the path? This will tell the browser to look from the root of the domain:
http://empresa.com/styles.css
In this way you can still decouple your HTML from the protocol and domain/port (so you aren't tied to http://empresa.com) but the path will always be the same regardless of the URL that was used to reach the referencing page.
Try this code :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)/([^/]*)/([^/]*)\.html$ /empresa.com/index.php?p=$1&id=$2&cat=$3 [L]

Strange php behavior

I was busy on my website doing URL rewrites when suddenly something happened from which I really don't know how it happened or how this is possible and how to maybe prohibit this from happening, probably some little mistake in the URL rewrite rule where I try to force the www in front of the address but when I remove the www. and press enter it comes up with an address which he can't find cause he doesn't put a / behind the .nl
somehow a extra index.php got fixed in between and what happens that in this way the whole website can be seen without any CSS markup...
it is reproducible, just fit the extra index.php in between like the example and the whole website can be seen and surfed without any markup from the CSS file
[http://www.capoeiravelsen.nl/index.php/index.php?page=home]
what is actually happening here ? why isn't it reading the CSS file in this way. of course users are not just gonna fix the extra index.php in between but does this presents a sort of security breach or bug or whatever....
[EDIT]
yeah it sounds logic that it can't fetch the CSS cause it looking in a
different directory or something, still don't know how it still can
fetch all the other docs then since a folder named index.php doesn't
exist in any way.
it happened with this rewrite rule:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} (.*)
RewriteRule (.*) http://www.%1$1 [R=301,L]
where I forced the www in the address, but when a some random crazy
user would want to delete the www. and pressed enter the weird effect
occurs.
the request would then change to:
[http://www.capoeiravelsen.nlindex.php/?page=home] where the browser
says it can't find that page of course
not noticing the / in between .php and ? I put a / in between .nl
and index.php so it would become
[http://www.capoeiravelsen.nl/index.php/?page=home] and pressed enter
then the whole address changed to:
[http://www.capoeiravelsen.nl/index.php/index.php?page=home]
where browsing the site is possible but without markup...
I solved this issue by changing the rewrite rule to:
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
when some random user would delete the www. and pressed enter nothing
crazy happens the www. just gets forced again
You are linking the CSS relatively
<link href="inc/stylesheet.css" rel="stylesheet" type="text/css">
When you are accessing /index.php/index.php?page=home the browser look for the CSS file in
/index.php/inc/stylesheet.css
The Solution is :
Fix the rewrite so it won't allow using "/" after the php file (this way the OS looks for folder named "index.php"
link the CSS file absolutly:
i.e:
[link href="/inc/stylesheet.css" rel="stylesheet" type="text/css" />
(replace the [ with < :) )
Hope it helps
It will try to fetch the CSS files from the directory /index.php/ on your web server and not / as expected. It is not a security breach
I don't know about your rewriting rules since you haven't posted them, but the problem with missing CSS is because you're linking the stylesheet and other files in your HTML with relative paths, i.e.:
<link href="inc/stylesheet.css" rel="stylesheet" type="text/css">
Since your URL is http://www.capoeiravelsen.nl/index.php/index.php?page=home that makes the browser request a file located under http://www.capoeiravelsen.nl/index.php/inc/stylesheet.css, which, of course, doesn't exist on the server.
One solution is to change all paths in your HTML to absolute, i.e.:
<link href="http://www.capoeiravelsen.nl/inc/stylesheet.css" rel="stylesheet" type="text/css">

Categories