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!
Related
I just recently found out about url rewriting and i wanted to change my url from search.php?s=keyword&p=pagenum to search/keyword/pagenum. Therefor i created these rules in my .htaccess file:
RewriteEngine On
RewriteRule ^search/([A-Za-z0-9-]+)/?$ search.php?s=$1 [NC,L]
RewriteRule ^search/([A-Za-z0-9-]+)/([0-9]+)/?$ search.php?s=$1&p=$2 [NC,L]
The redirecting part works, i just noticed that my pictures on the search results are missing. This is my folder layout:
root/
---search.php
---images/
---{id}.jpg
in my search.php i access images like this: <img src='images/" . $id . ".jpg'>. Can anyone explain to me why this is happening and if possible how to fix it? Thanks in advance!
err, try
img src='../images/$id.jpg'>
or
img src='/images/$id.jpg'>
This is a link on index.php page
<?= $mbbelow['brand_name']; ?><?= $mbbelow['title']; ?>
from this link I come on page specification.php
then this url come http://www.themobilesapp.com/specification.php?url=Sony-Xperia-Z5-specifications-5246.php
in the base of url I find all data of page.
This url is not proper according to me.
Here I want to remove specification.php?url= from this above url.
Please tell me in proper and full explain way that how to remove and make new url like this
http://www.themobilesapp.com/Sony-Xperia-Z5-specifications-5246.php
I only need .htaccess or I also need to work using php code to remove this.
I will provide you all details you need here for to remove this.
Please try this in .htaccess file:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ specification.php?url=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ specification.php?url=$1
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've googled for some mod_rewrite tutorials, but couldn't figure out how to solve my specific problem regarding the way I created my site with includes.
I've an index.php and in this file there are several includes like index.php?s=events will include events.php and so on.
If I use this code:
RewriteEngine on
RewriteRule ^/?([a-zA-Z_]+)$ index.php?s=$1 [L]
it will result that www.mydomain.com/events just shows the events.php itself, not with the "skeleton/framework" index.php around it, like it should be. So it just loads the specific included file.
My target is to call www.mydomain.com/events for example to show the whole index.php?s=events page.
Try putting slash before URL into the RewriteRule:
RewriteEngine on
RewriteRule ^/?([a-z_]+)$ /index.php?s=$1 [L,NC]
I added NoCase flag to get rid of capital letters in the regexp.
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]