Php includes and images not working for Rewritten urls - php

After editing htaccess file for the short urls ,now my page has broken and php included files and images are not working,
This is my htaccess in the public_html
RewriteRule ^car/name/(.*)$ /car.php?name=$1 [L,QSA]
All included files and images are in /files folder ,
Now the problem is when I visit
http://example.com/car.php?name=hundai
it works and everything is ok,
but the problem is with Rewritten url
http://example.com/car/name/hundai
It breaks all php files and images.
Can you please help a fix for this?

RewriteRule is changing your URL http://example.com/car.php?name=hundai into http://example.com/car/name/hundai ,which means that files path is inside a directory it was not before. this means that any relative links in document will not work. To solve this You need to use an Html
Base
tag inside the "head" of your document.
<head>
<base href="http://example.com/files/">
</head>

Related

How to update absolute URLs for media files for a whole site at once?

I have moved a whole site to a new domain. However, some of the <img src> urls are absolute and are no longer pointing to the files in the correct
(new domain) location. The relative URLs are working, since the file structure is the same as the original domain. How can I redirect the URLs for the media files in one place for all the page files?
I have tried to set redirects in the .htaccess file but no luck.
Here is the most current version of the redirect I tried to use, but it seems to only works for page urls, not the media absolute urls in the body of the pages:
#Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*)$ http://www.newsite.com/$1 [R=301,L]
I am looking for detailed instruction on where to put code etc. to redirect all of the absolute URLs in my site. I am a rookie, so please be clear and kind. Thank you for your help.
separate the files in sizable chunks based on your pc's capabilities into folders and open them all in a text editor - search and replace, then go from there. Let me know if this helps or if I am completely off my rocker. : )

my php file doesnt link to css when i clean url using .htaccess

When I use rewrite rules to clean my URL, I can't access my CSS files. Without the rewrite rules my page works fine, but with the rewrite rules images,CSS and JS documents cannot be linked. What could be the problem?
My .htaccess codes
RewriteEngine On
RewriteRule ^index?$ index.php
RewriteRule ^product_details/([0-9]+) product_details.php?id=$1
Now I don't understand why my edit.php is unable to locate external files even though it loads from the correct path on server-side (mysite.com/). And it does not show the URL in an extra directory (in this case mysite.com/e/).
Mod Rewrite will redirect www.example.com/product_details/123 to product_details.php?id=123 on server-side.
Your browser thinks that /product_details/ points to a directory and resolves relative links accordingly.
The solution is to use absolute paths or add <base href="//www.example.com/"> to your html head element.

htaccess url rewrite rule not working as expected

I have a complex problem that I an unable to solve for days now. Maybe some expert with more knowledge of htaccess functionality will be able to help out.
I have two files placed in the root directory - test.php and files_include.php.
The URL that a user would normally see is:
www.example.com/test.php?cs1=A&cs2=B&cs3=C&cs4=D
Since this is a ugly URL I would like to rewrite it to something better like:
www.example.com/search/A-B-C-D.html
Using a rule in .htaccess like this I can easily rewrite the URL:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^search/([^-]*)-([^-]*)-([^-]*)-([^-]*)\.html$ /test.php?cs1=$1&cs2=$2&cs3=$3&cs4=$4 [L]
In the file test.php I call for the website config files like this:
include('files_include.php');
Now the problem. As soon as I rewrite the URL to a location different from the root one, I get a really strange issue. The page still renders correct in browser but:
Problem 1. I have to replace src="images with src="../images if I want to see the image correct. This can be easily corrected by giving an absolute link, it is the easier part to do.
But the question is why is the relative path changing? Is .htaccess making the browser think we are in search'/ folder? The answer to this question will help me to identify the main issue, which is Problem2.
Problem 2. Sitemaps generators cannot follow the links on the page once the URL is rewritten, as if it appears blank to them, no matter that in browser all looks fine.
Therefore I am guessing that by rewriting the URL to search/A-B-C-D.html I am breaking something with the inclusion of files_include.php.
Basically, I need a general idea of were to look at and the things I should have in mind when rewriting root/test.php to root/search/A-B-C-D.html
Any suggestions?
Your browser is clueless about 'pretty' and 'ugly' urls. It just requests a folder or a file. If you request http://example.com/search/A-B-C-D.html, to the browser you are requesting a page A-B-C-D.html in the /search/ folder. If you have any relative urls on that page, it will request them relative to that /search/ folder. The browser has no clue, and should have no clue, what the internal representation of a request looks like. Heck, at your end of the line it might even be translated to instructions for a colony of hamsters, which will then send correct data through. The browser doesn't need to know how hamsters behave ;-)
The first problem is easily resolved by making your urls absolute. I wouldn't recommend making them relative to the pretty url. An alternate solutions would be to add the <base> tag to the <head> tag of your page. The href property of this tag will be used as a base for any relative links on your page. See mdn for more information. You would then do:
<head>
<base href="/">
</head>
As for your second problem, the include itself is not the problem. include(..) will first try to find the file in the include_path, and otherwise in the script's directory and the working directory. This doesn't change if you create pretty urls. Apache, and php, still know where the actual file is located you are executing. If an include statement fails to load a file it will generate an error too, which is another way you can tell if the include itself is the problem. See the documentation.
But the question is why is the relative path changing? Is .htaccess making the browser think we are in search'/ folder? The answer to this question will help me to identify the main issue, which is Problem2.
It's changing because the browser is loading /search/something-something-sometrhing-something.html instead of /test.php. The first URL has a relative URI base as: /search/ and the second URL has a base of /.
For the second problem, you could try externally redirecting, but not sure if that'll help the sitemap itself, it depends on the generator. Try adding this rule:
RewriteCond %{THE_REQUEST} \ /+test\.php\?cs1=([^&]*)&cs2=([^&]*)&cs3=([^&]*)&cs4=([^&\ ]*)
RewriteRule ^ /search/%1-%2-%3-%4.html [L,R]

Klein system. CSS isn't working

I'm testing Klein routing system, https://github.com/chriso/klein.php
it's awesome but i can't get my css ant images to run
This is my directory structure:
index.php
.htaccess
vendor
views
includes
assets
css
images
And here's one of my includes line of code where i try to access my assets/css:
<link href="../assets/css/main.css" rel="stylesheet">
I tried everything. Every possible hint would be great, thanks.
EDIT:
now i think it can be problem in my htaccess. how to make that url won't rewrite if it's css or img?
my .htaccess :
RewriteEngine on
RewriteRule ^(.*)$ index.php?rt=$1 [L,QSA]
Your link is most likely wrong (unless the code you pulled it from is in the include or assets folder). the ".." literally means "up one level". So if you code is in the .htaccess folder (which it usually is I believe) or any folder on that level the correct link would be:
href='../views/assets/css/main.css'
If your code is in css or images (not sure why it would be) the correct link would be:
href='../css/main.css'
Good lcuk!

Hide file extension with htaccess

I'm not the best with mod rewrite so if anybody can help me out here that would be great.
I'm using a markdown processor script and it's using rewrite to grab any files that end with a markdown file type. However, I'd like this script to grab any files within a folder, rather than any files that end with the markdown file type.
Here's the htaccess:
# display Markdown as HTML by default
RewriteEngine on
RewriteRule .+\.(markdown|mdown|md|mkd)$ /static/includes/markdown/render.php
RewriteRule .+\.(markdown|mdown|md|mkd)\-text$ /static/includes/markdown/render.php [L]
Is there a way to grab all files within a folder called (let's say) "folder" and eliminate the file type on the end?
So maybe have a URL like
website.com/home
that actually is
website.com/home.md
and is processed with the markdown script?
Hope this makes sense.
The re-write module and it's .htaccess files actually work on a per folder basis. Usually one would have a main .htaccess file in the web root of a site/server. However you can add numerous .htaccess files throughout your site's folder structure giving each individual folder specific rules.
All you would have to do is add another .htaccess file to your markdown folder and enable it to parse URL's without file extensions, forwarding it to a script which will be able to detect what original file was requested -
RewriteEngine on
RewriteRule ^(.*)$ /static/includes/markdown/render.php?file=$1 [L,QSA]
Basically what is happening here is that any file requested within this folder will be passed through your render.php file.
Now in your render.php file, you would have a $_GET parameter of file containing the original URL. For a url of http://example.com/markdown/foo, your render.php would have foo in the file parameter -
/static/includes/markdown/render.php?file=foo
If you set the correct headers in render.php it will be able to print out any format of file, hiding it's extension in a "fake" URL.

Categories