mod_rewrite and not displayed picture - mod-rewrite

There is index.php and logo.gif files in my server.
I need to change the way of display an adress from example.com/?nr=33&id=foo to example.com/33/foo or example.com/33/foo/. I have type in .htaccess this code
RewriteRule ^([^-]+)/([^-]+)$ /?nr=$1&id=$2 [L].
It almost works, but when I use example.com/33/foo logo.gif (with is used in index.php) dissapears in a browser. Can you show me mistake in it?

When you enter a URL like example.com/33/foo in your browser, your browser will assume it is in a directory of that name, and request the (relative) URL logo.gif as
www.example.com/33/foo/logo.gif
Reference the image as
/logo.gif
and it should work.

Related

Silly 404 page usage?

Alright, so the only way I know of changing links to not show the file extensions;
this yourwebsite.com/customlink.php to yourwebsite.com/customlink
is either creating a folder called customlink and shoving an index file in there. Or creating a 404 page which snoops around the URL and grabs whatever string is behind the last / and does whatever to show the proper content.
My question is if this way of solving the problem is straight out idiotic, or not? I'm doing this because in my mind it saves space, let me explain: Every page that needs a customlink are the same with some bits and content taken from other places, so instead of creating X amount of folders and index files that includes a main file, I'll just have one 404 file that can handle it.
I apologize in advance if this is really stupid
As you are not mentioning what server your pages are running on (Apache, IIS, ...?), I'll just assume Apache.
Put an .htaccess file into the root of your site with the following content:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
It will internally rewrite everything without an extension to the corresponding php file, provided that:
the request is not a valid directory
a file with a php extension is in fact present
A more common approach is to route all HTTP requests to a single index.php using mod_rewrite in a .htaccess.
Then, based on the requested resource, the index.php outputs the appropriate file, whether it is generated from a database or nested in some other folder.
It's probably not a good idea to use a 404 page (an error page) for anything other than "File Not Found" instances.

mod_rewrite proxy URL missing images/css

I have the following mod_rewrite using Proxy flag to redirect from one URL folder to another site subdomain as follow:
The .htaccess file placed inside http://www.domain.com/test/ folder:
RewriteEngine on
RewriteRule ^($|/.*) http://subsite.site.com/$1 [L,P]
The problem, images, CSS and links are not showing up properly. Links appear to be pointing back to: http:// www. domin .com/linkname.html
I've tried doing RewriteBase /test/ and / with no luck, and couldn't figure out any other way to do it.
What am I missing in above code to make it work with relative paths at destination URL?
Oh, you want to change internal content?
Mod_rewrite only changes headers, not content and you would definitely need something else like mod_proxy_html. However, rewriting content just to change urls can normally be completely avoided (assuming you have control of your content) by making all paths legitimately relative. In such cases all paths in content should be like: linkname.html or some_path_from_here/linkname.html instead of /linkname.html or some_path_from_here/linkname.html

Change URL in address bar but keep on same page

Is it possible to rewrite a url so that the page stays the same and the url itself is chanaged ?
E.G.:
I have a page at www.example.com/sales
I want this url to appear in the address bar as www.example.com/sales_and_repairs
I am NOT trying to redirect a page at www.example.com/sales to ANOTHER page at www.example.com/sales_and_repairs ....There is only ONE page - it is just the URL I am trying to change so that if a person types in www.example.com/sales, they will go to that page but the URL in the Address bar will change to show as www.example.com/sales_and_repairs
Is this possible with rewrite rules ? Anytjhing I have looked up appears to suggest that you have to be redirecting to a second page - but that is not what I want to do - I just want to change the actual URL.
Any advice please ??
If you want to redirect www.example.com/sales_and_repairs to www.example.com/sales permanently you can do it with an .htaccess file.
First of all, you'll have to enable mod_rewrite in apache.
Then add the following to your .htaccess file :
Options +FollowSymlinks
RewriteEngine on
Rewriterule ^http://www\.example\.com/sales_and_repairs$ http://www\.example\.com/sales [R=301,NC,L]
This method allow you to have only one file behind the two urls.
However if you want to modify the url after a user's action, you can do it with the answer given by Sparda above.
Seems you can achieve this with javascript :
location.hash = 'newurl';
But this will add an anchor to the url.
Some earlier features of html5 can do this but are not really supported yet :
history.pushState(data, 'title', 'newurl');

htaccess redirect raw image viewing

I'd like to redirect any image that is viewed directly to a handler page:
if http:// mysite.com/pics/filename.jpg (or www.)is in the URL
then redirect to http:// mysite.com/pics/index.php?img=filename.jpg (no www)
But if the image is being called by a webpage or a mobile html5 app anywhere then it should be served per normal.
So if mypage.html contains an img tag with the direct photo in it it will be shown in that page. But if http:// mysite.com/pics/filename.jpg is the url then it should redirect. In other words if the file is being viewed directly it should redirect to the wrapper page, but if it's already in a wrapper page (anywhere) it shouldn't redirect.
I've seen various redirect code but none that that references the visible url for the if statement, so I don't know how to do this. And the ones I've found and tried don't work, either redirecting all requests, or not doing anything
Thanks!
This will take care of people hotlinking to images on your site for you:-
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?mysite.com [NC]
RewriteRule ([^/]+)\.(jpg|jpeg|png|gif)$ pics/index.php?img=$1.$2 [NC,L,R]
As for your mobile HTML5 app you are going to need some other way to identify it to your web server. So you are going to have to see what referer that says it is when it is asking for images. It should be pretty easy for you to write a PHP script to tell you that, then you just need to add to the above code to include that.
Simon

Remove part of URL only for certain kind of files with mod_rewrite

I need your help. I have a Joomla site working. I enabled it friendly urls and it works fine. All rewritten URLs are this way
http://mydomain.com/start/article-page-well-rewritten
When I activate Joomla Cache plugin, when I load page first time, it works fine, but afterwards, it doesn't load any css or image file.
Debugging resulting html, I realized cached file has these images and CSS links
mydomain.com/start/images/coolimage.jpg
mydomain.com/start/css/stylesheet.css
Instead of
mydomain.com/images/coolimage.jpg
mydomain.com/css/stylesheet.css
(real routes)
So I think I need a rewrite rule to remove word "start" from url, and then retrieve them and show, but only to image and css files from mydomain.com/start/
Can you help me how to do it? I don't want anything else to be rewritten...
RewriteRule ^start/(images|css)/(.+) /$1/$2 [R]

Resources