URL rewriting messes with accessing file - php

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'>

Related

Remove file name “specification.php?url=” from url

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

mod_rewrite issues and php

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

Using a variable in the address bar to display an image?

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!

RewriteRule - html broken - Like missing css

I have problem with this rule:
RewriteRule ^([^/]+)/(\d+)$ more.php?books=$1&tags=$2 [L]
After this rule everything works but page is displayed incorrectly. Like missing css.
Thanks in advance
This might happen, when you have a relative URL to your CSS files. If you access the URL /book-name/2 and have a link to css/style.css, for example, the browser will resolve this to /book-name/css/style.css.
Change your CSS links to absolute URLs, like /css/style.css, and you should be fine.

mod_rewriter change my url

I have some problems with mod_rewrited at .httacess.
We have created a website, the website links have this sctructure.
www.myweb.com/page.php?title=this-is-the-title&filmID=454122
AND
www.myweb.com/video.php?title=this-is-the-title&filmID=2567971&player=veevr
AND
www.myweb.com/gallery/index.php?galeriID=11
is possible to change this structure to:
www.myweb.com/page/this-is-the-title/454122.html
www.myweb.com/video/this-is-the-title/454122/veevr.html
www.myweb.com/gallery/index/11.html
Any help will be really great
Thank you for reading the post!
Best Regards
JoinOG
In your .htaccess file in your web root folder put the following code.
RewriteEngine On
RewriteRule ^page/([^/.]+)/([0-9]+).html/?$ page.php?title=$1&filmID=$2 [L]
RewriteRule ^video/([^/.]+)/([0-9]+)/([^/.]+).html/?$ video.php?title=$1&filmID=$2&player=$3 [L]
RewriteRule ^gallery/index/([0-9]+).html/?$ gallery/index.php?galeriID=$1 [L]
What this does is mask the url /page/something/12345.html to page.php?title=something&filmID=12345. It masks it, so when you go to the first URL it still looks like the first URL in the address bar but is really at the second URL. Simple tutorial on how this works is here: http://corz.org/serv/tricks/htaccess2.php
The server will think you are in the folder /page/something/ so if your CSS, images and hyperlinks are locally relative links they will not work, e.g. it will look in /page/something/yourimage.png for an image linked to like this <img src='yourimage.png'/>. To get it to work as you'd like it to, you'll need to put a forward slash before all your links to make it relative to your website's root folder like this <img src='/yourimage.png'/>.

Categories