if request to my site goes like:
my1stdomain.com/request-1/
my1stdomain.com/any-another-request/
it should open web-pages:
my2nddomain.com/folder/request-1.php
my2nddomain.com/folder/any-another-request.php
I put php file in the web-root of my1stdomain.com with name referer.php
and it looks like:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} /$ [OR]
RewriteCond %{REQUEST_FILENAME} (html|php)$
RewriteCond /home3/juraj1/public_html/referer.php -f
RewriteRule ^.*$ /referer.php [L]
</IfModule>
and referer.php looks like:
<?
$login=preg_replace('/\//i','',$_SERVER['REQUEST_URI']);
$html=file_get_contents('http://my2nddomain.com/folder/'.$login.'.html');
echo $html;
?>
but it returns only html content without css styles and images
Can you please help me with it?
I assume the HTML files on my2nddomain.com link to the css files/images in a relative way, like <img src="images/picture.jpg" />. But since this image does not exist on my1stdomain.com, the browser doesn't find it.
Solution 1:
Link to all images/css files with an absolute path, like <img src="http://my2nddomain.com/images/picture.jpg" />
Solution 2
Add the <base> tag to the HTML head of all the files on my2nddomain.com like this: <base href="http://my2nddomain.com" />. This can be achieved by changing the HTML on the fly like $html = str_replace('</head>','<base href="http://my2nddomain.com" /></head>',$html);
There are also other solution with rewriting every request to a css file/image on your 1st server to the corresponding file of the 2nd server, but this will be more complicated.
Related
I'm trying to convert my app links, so that a link like this:
http://localhost/index.php?id=13&category=Uncategorized&title=just-a-link
gets converted to this:
http://localhost/13/Uncategorized/just-a-test
so far I was able to do it using:
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ /index.php?id=$1&category=$2&title=$3 [QSA,L]
but that completely breaks links to css and other files as it redirects every request with query to index.php
so I changed it slightly so that it only runs when the first query is a number:
RewriteEngine On
RewriteRule ^([0-9]*)/([^/]*)/([^/]*)$ /index.php?id=$1&category=$2&title=$3 [QSA,L]
this one doesn't break css and js files, however when you go to the link for example http://localhost/13/cat/test then try to go to another link like http://localhost/19/Uncategorized/something by clicking on it inside the previous page it will instead redirect you to something like http://localhost/13/Uncategorized/19/Uncategorized/just-a-test
How do I achieve what I described without any of these weird side effects??
Use :
RewriteEngine On
RewriteRule ^([0-9]+)/([^/.]+)/([^/.]+)/?$ /index.php?id=$1&category=$2&title=$3 [QSA,L]
And add this code to your html <header>:
<base href="/">
OR
<base href="http://www.domain.com/">
To fix relative css and js links
I have a little problem I can't seem to get my head around. What I'm trying to do is that when someone "http://domain.com/directory/ImgVariableHere" it will display the image on a php page. I know I'm probably going about this the complete wrong way and that's why I've come here. I'm a newbie to PHP so please forgive me. Here's the code I have.
<?php
$img="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
echo "<img src=\"$img.png\">\n";
?>
Here's the code it echos.
<img src="http://domain.com/directory/.png">
I'm probably explaining this really badly, ask any questions of you need to. Thanks. If you have a completely different and better way of doing it then please do tell.
EDIT: What I'm trying to do is kind of like Gyazo. They show their images on a webpage, I'm trying to do that but by using variables in PHP and I just can't wrap my head around it.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [PT,L]
</IfModule>
Put this in .htaccess in the root of your document, and it should route all requests to index.php, then you just put the script above in index.php, and it should work.
It looks like you want to have a RewriteURL with a variable in it.
So when you want this we can create a .htaccess which will reserve a spot for your variable. Example is like this:
RewriteEngine on
RewriteBase / #Maybe your in a subfolder? Change this to /yoursubfolder/secondsub/...
#Redirect all urls that begin with /image/ to image_shower.php?image=<image value>
RewriteRule image/(.*) image_shower.php?image=$1
This rewrite all the urls that begin with /image/ to the image_shower.php without showing the image_shower.php URL. It also sends the information in a GET variable. Which can be easily retrieve in PHP. Like this:
<?php
$img="http://".$_SERVER['HTTP_HOST']. '/' . $_GET['image'];
echo "<img src=\"$img.png\">\n";
?>
Hope this will help you go further!
im still new to .htaccess
so i looked on stackoverflow for my answer and i cant find it. i have rules for my site i had to setup so i got my rules from this:
SEO Friendly URL to Dynamic URL using PHP
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?request=$1 [QSA,L]
but for some reason my css is broken but my php is good
example url:
http://www.domain.com/aboutme
http://www.domain.com/css/version.css
The problem is due to Capitalized file name Version.css.
If your devbox is windows it'll work. Windows is case insensitive.
But when you test it in server (Unix) it will break.
Because Unix is case sensitive
Better rename Version.css to version.css. Its safe for the future
I have a super answer for this problem. I've been looking everywhere for an easy solution that wouldn't make me hardcode baselinks for all my relative paths, like imgs, css, javascript...
So here it goes, add this between the <head> tags of the pages you are having problems:
<base href='http://www.mydomain.com/'>
This will make that your relative path links will start with this base link. Simple as that.
The <base> tag specifies the base URL/target for all relative URLs in a document. Put the <base> tag as the first element inside the <head> element, so that other elements in the head section uses the information from the <base> element.
Did it work for you?
I have an htaccess file in the root that redirects every request of a page to a specific file:
RewriteCond %{REQUEST_URI} !^/loader.php(.*)?$ [NC]
RewriteRule ^(.*)$ /loader.php?url=$1 [QSA,L]
Now the redirect is easy in the loader.php
include($_SERVER['DOCUMENT_ROOT'] . '/' . $_GET['url']);
I just include the URL,admin/index.php for example.
If I leave the code as is the loader will include the file and print the correct HTML, but it will not load any CSS or JS scripts. If I put echo "test";
just before the include, the loader will load the CSS file. It's something that is killing me. Do I have to specify something in the HTTP header?
I already tried putting <base url="" /> in the header of index.php with no result,
but another strange thing is that with Chrom if I inspect the page and click on the link I'll see the right CSS.
Update 1
I printed the headers_list();. I noticed one thing - when I print an echo in headers_list, an array shows the content-type, so I tried to add it on my own with the header() function but with no result. Still working on it.
Update 2
I've noticed another thing; if I put a <style></style> tag with some CSS it will work fine, but if I use the <link /> tag it doesn't. This doesn't make any sense.
You have to send the correct MIME headers for css and javascript (and images etc.).
Easiest is to just let apache handle those requests. Put all the CSS, JS and images in a folder named 'assets' or something and change the htaccess to
RewriteCond %{REQUEST_URI} !^/assets/
RewriteCond %{REQUEST_URI} !^/loader.php(.*)?$ [NC]
RewriteRule ^(.*)$ /loader.php?url=$1 [QSA,L]
I have just one root directory with index.php in it along with two folder img and css.
I refer to files in this folder like:
src="img/path.png" i.e relative to the root directory.
The other day I had some mod_rewrite question & this is what someone gave me, which seems to wrok fine except for trailing slashes and css/img breaking apart
RewriteRule ^$ index.php?page=1 [L]
RewriteRule ^([0-9]+)/?$ index.php?page=$1 [L]
RewriteRule ^([A-Za-z]+)/?$ index.php?category=$1&page=1 [L]
RewriteRule ^([A-Za-z]+)/([0-9]+)/?$ index.php?category=$1&page=$2 [L]
This is what is bothering me:
Using Rule1:
www.example.com changes to www.example.com/index.php?page=1 which is great
Also www.example.com/ some how changes to www.example.com which is again great
Using Rule2:
www.example.com/2 changes to www.example.com/index.php?page=2 like what I would want
But using www.example.com/2/ (TRAILING SLASH) also retrieves page=2 but somehow the img and css breaks apart.
I am guessing the problem is with url being treated as directory structure and then it cant find img and css folder there.
Using Rule 3:
www.example.com/Football changes to www.example.com/index.php?category=Football&page=1 again like what I would want
But www.example.com/Football/ (TRAILING SLASH) suffers from the same problem with img and css breaking apart
Using Rule 4:
www.www.example.com/Football/2 even without the trailing slash breaksdown on css and img however the page can retrieve tha page and category correctly.
How do I correct this problem without having to use absolute paths in my html.
Please advise on the trailing slash problem as well.
Can you not reference your images/css using relative but starting with a /?
/images/blah.jpg
/css/style.css
You could add a rule like:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|robots\.txt|favicon.ico)
# Other rules here
The idea is to avoid the rewrite case for an existing file or directory.
With a trailing slash, the path will be interpreted as a directory. Wherever you include images, css files and such you have to change the paths. Say you have images in an img folder, you probably use it in css like:
background: url(img/image.png)...
Change that to :
background: url(/img/image.png) ...
Same goes for everywhere you have images referenced or other paths. Using a path that starts with "/" means that you are searching from the root directory. The rules you're using are the rules that I'm ussually using and didn't have any problems with them. Actually, I find it easier to build my paths with a starting "/".
Just as a last example, if you have: mysite.com/home and you have <img src="img/image.png" /> the image will be searched in mysite.com/img/image.png which is probably what you want.
But if you have mysite.com/home/ and <img src="img/image.png" /> the image must reside in mysite.com/home/img/image.png which is not what you want, thus using <img src="/img/image.png" /> would be the solution for you.