Rewrite rule for one image - php

I've got an .htaccess file that reads as follows:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
There's an image on index.php that should always display, however when I navigate to a rewritten path, the request for the image is prepended with that path. So the "categories" section, while it should still display my foo.png at images/foo.png now has the path categories/images/foo.png
Can I fix this strictly with .htaccess rewrite rules? If so, how?

A more simple solution would be to use a src for the img-element in your html based on the root-folder of your site. (If it's just for that one image...)
For example:
http://www.site.com
<img src="/images/header.jpeg" />
This will always refer to http://www.site.com/images/header.jpeg, no matter what folder you're in.

This should take any request for the image and rewrite it to go the root /images/foo.png:
RewriteRule ^.*/images/foo.png$ /images/foo.png
Note that if you have a different image located at somewhere/images/foo.png, requests for that will also be rewritten to /images/foo.png, unless you write specific exceptions for it.

This sounds like a relative vs absolute URL mix-up. When you have paths like http://domain.com/category/post/name the relative URI base is /category/post and all relative links that get served in the content returned by the webserver will have that prepended to it by the browser. In order to tell the browser what the real URI base is, you can include this in your headers:
<base href="/">
Or you can simply change all your relative URLs to absolute URLs.

Related

.htaccess pretty URLs are not working

I'm trying to use pretty URLs on my site, and I don't know too much about htaccess, but after some search I ended up with this:
RewriteEngine On
RewriteRule ^([a-z]+)\/([0-9]+)\/?$ content.php?action=$1&id=$2 [NC]
I'm trying to change this:
mysite.com/cms/content.php?action=content&id=80
into:
mysite.com/cms/content/80
But somehow it's not working and what I get is a blank screen and a 404 error,
Failed to load resource: the server responded with a status of 404 (Not Found) --in-- /cms/files/plugin/tinymce/tinymce.min.js
About the 404 error: It should load a .js file from mystie.com/files/plugin/tinymce/tinymce.min.js -- not -- mystie.com/cms/files/plugin/tinymce/tinymce.min.js
My htaccess file is in the root folder. Am I doing something wrong?
I think the server did not read my .htaccess file. How do I find out the issue?
You need to place this rule in /cms/.htaccess:
RewriteEngine On
RewriteBase /cms/
RewriteRule ^([a-z]+)/([0-9]+)/?$ content.php?action=$1&id=$2 [NC,L,QSA]
Then you need add this in the <head> section of your page's HTML: <base href="/" /> so that every relative URL is resolved from that URL and not the current page's relative URL.
RewriteEngine On
RewriteRule ^/cms/([a-z]+)/([0-9]+)$ content.php?action=$1&id=$2 [NC]
Remove escape slashes \
You need to make sure to include the subfolder if your htaccess is in the root.
Try this rule.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^cms/([a-z]+)/([0-9]+)/?$ /cms/content.php?action=$1&id=$2 [NC,L]
Also a blank screen usually means a PHP error.
You have /cms/ in your URL before the rest, so you need to change RewriteRule to something like this:
RewriteRule ^cms/([a-z]+)\/([0-9]+)\/?$ content.php?action=$1&id=$2 [NC]
Also, I hope you are aware that .htaccess can not work on some servers (like nginx or some custom ones), and even on Apache servers there can be mod_rewrite disabled.
My guess is that your scripts and styles are being linked to via relative URLs. Because the old URL is /cms/content.php the URI base is /cms/ and your relative links all resolved to be under /cms/, which I assume is what you want.
However, when you change your links to /cms/content/1234, the URI base becomes /cms/content/ and all relative links will use this as its base, and since "content" isn't even a real folder, all those links will 404, and you won't load any of your CSS content or scripts.
Either change your links to absolute URLs or specify a URI base in the header of your pages (inside the <head> </head> tags):
<base href="/cms/" />

.htaccess mod_rewrite does not work with two parameters on localhost

I would like to have clean URLs in my projects. So I've written these codes in a .htaccess file.
RewriteEngine On
Options +FollowSymLinks
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ index.php?page=$1&id=$2 [NC,QSA,L]
RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [NC,QSA,L]
But it does not work completely when I'm trying to work with it locally.
Imagine that I have a directory myproject in htdocs (www) in my local web server path and other files are stored in this folder. Now I can see the project if I go to localhost/project.
Now I want to work with URLs.
It works well if I have only one parameter in URI like localhost/myproject/tours. But if I have 2 parameters like localhost/myprojects/tours/inside, it seems that all css, js and images files go away. I've also added RewriteBase /myproject to .htaccess file, but nothing solved.
What is my mistake? I need a solution that works on both remote and local server.
First of all, see my response on your other question about your code: Why .htaccess mod_rewrite does not show a real directory
Now, RewriteBase won't solve your problem about css/js/images etc. It's only for htaccess and it defines the base path when a rule is rewritten.
One common way to avoid this problem is to add in all your files a base url right after <head> html tag.
For you, it would be: <base href="http://localhost/myproject/" />
Otherwise, if you reach localhost/myprojects/tours/inside then your css/js/images links will be resolved as localhost/myprojects/tours/inside/__here__ because the default base path here is the current directory (/myproject/tours/inside/) and this is not what you want
Edit: if that's the case, don't forget to remove leading slashes from your css/images/javascript html links
The browser will build absolute URL paths out of your relative URL paths by looking at your made up context of /myprojects/tours. You may need to strip one or two levels of that prefix off to find the real path.
The access log will show you plain as day what relative URL's come in when you use the old and new URLs.

Access relative CSS/JS files with htaccess rewrite rule

I was recently asked if I could make my friends server address more user friendly. His current urls looks like this:
http://wwww.example.com/site/index.php?page=home
http://wwww.example.com/site/index.php?page=about/john
http://wwww.example.com/site/index.php?page=portfolio/concept-art/2013
He would like them to look like this
http://wwww.example.com/site/home
http://wwww.example.com/site/about/john
http://wwww.example.com/site/portfolio/concept-art/2013
which I thought would be pretty simple so I wrote this following rewrite.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !/(admin|css|fonts|ico|include|js)/
RewriteRule ^([^_]*)$ index.php?page=$1 [L]
which seems to work for the basic links like
http://wwww.example.com/site/home
but for something like this
http://wwww.example.com/site/about/john
none of the css or js will load. So I fixed that for now by making all of the files absolute paths but I am worried that my friend is going to add a new plugin or something and forget that he has to make it an absolute path.
My Question
Is there something I could change or add in my htaccess file to get the css and js files to load with a relative path? If so what would I need to do?
Better to use this rule:
RewriteEngine On
RewriteBase /site/
RewriteCond %{REQUEST_FILENAME} !/(admin|css|fonts|ico|include|js)/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)/?$ index.php?page=$1 [L,QSA]
Then for css/js/images better to use absolute path in your css, js, images files rather than a relative one. Which means you have to make sure path of these files start either with http:// or a slash /.
Alternatively You can try adding this in your page's header:
<base href="/" />
OR
<base href="http://domain.com/site/" />
Is there something I could change or add in my htaccess file to get the css and js files to load with a relative path? If so what would I need to do?
You could just add the proper relative URI base in your page header:
<base href="/site/" />
Or you could brute force redirect them using mod_rewrite (not preferable):
RewriteRule ^(.+)/(admin|css|fonts|ico|include|js)/(.*)$ $2/$3 [L]

Mod_rewrite website like facebook

I'm using mod_rewrite for the first time in order o create a website similar to facebook.
Whener I type mywebsite.com/user.name, the mod_rewrite redirects me to mywebsite.com/hotsite/index/php and there, I use a little php to get the user name from the url and get the userId from it.
Then I have the other areas like mywebsite.com/user.name/diary, mywebsite.com/user.name/contact, and so on...
This is all working well with this code in my .htaccess:
Options +FollowSymlinks
RewriteEngine on
RewriteBase /mywebsite
# ————————————————————————-
# > URL REWRITING
# ————————————————————————-
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)/diario$ hotsite/diary.php
RewriteRule ^(.*)/recados$ hotsite/messages.php
RewriteRule ^(.*)/fotos$ hotsite/photos.php
RewriteRule ^(.*)/videos$ hotsite/videos.php
RewriteRule ^(.*)/contato$ hotsite/contact.php
RewriteRule ^([a-z0-9._\-]+)$ hotsite/index.php [L]
The problem I have is with the path to the external files (css, images, backgrounds...).
Since my browser thinks I am in "website.com" I had to add "hotsite/" to all the paths. This works well for when I am at the user main page, like "mywebsite.com/user.name". However, if I go to "mywebsite.com/user.name/diary" the browser thinks I'm in another folder and then I have to add "../hotsite" in order for the paths to work.
I could make an IF in all the paths to check if I am at the index or not, but this would be very clumsy.
I could also put absolute paths in evertyhing, but since I'm developing offline with apache, this wouldn't be good either.
Any other solutions?
Thank you vey much.
You should probably use the base tag available in HTML (put it between the <head></head> tags):
<base href="yoursiteroot" />
In other words, something like this:
<base href="/mywebsite" />
However, this requires that your relative links are adjusted based on the path you specify. :-)

Htaccess rewrite issue

I tried to use seo friendly url for my website, my target was to rewrite product/131/ABCDEFGH to productpage.php?id=131
I achieved this now it rewrite path and open correct page
.htaccess code
RewriteEngine on
RewriteBase /
RewriteRule ^product/(([^/]*))/([^/]*)$ /cprrpc/productpage.php?id=$1 [L]
Now I got another problem which is something related to internal links, currently all images etc on this page are binded with relative path.
Now this will not work, because they all are searching in different path,
I open on another approach or to change htaccess file.
Your URLs gets skewed on the rewritten URL structure as there's now two more directory levels. Instead of using relative URLs to the current directory use a leading slash for your resources OR you could use PHP to print the "root path" to your application in front of all your resources.
For example instead of using a relative URL (to the current directory):
<img src="images/plus.png" /> (expands to localhost/cprrpc/product/131/images/gplus.png)
you could use a leading slash which makes the URL relative to the web root:
<img src="/cprrpc/images/plus.png" /> (expands to localhost/cprrpc/images/gplus.png)
OR you could print the web root using PHP:
<img src="<?php echo $WEB_ROOT ?>/images/plus.png" /> (expands to localhost/cprrpc/images/gplus.png)
You need to allow apache to serve existing files from their real location:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^product/(([^/]*))/([^/]*)$ /cprrpc/productpage.php?id=$1 [L]
This means apache will ignore the rewrite if an image is requested..

Categories