my .htaccess file looks like:
RewriteEngine On
RewriteRule ^([^/]*)$ index.php/?node=$1 [L]
now when I include my css file with:
<link href="sitewide.css" rel="stylesheet" type="text/css" />
it doesn't work. The same goes for javascript.
I've been looking everywhere for an easy solution that wouldn't make me hardcode baselinks for all my relative paths, like imgs, css, javascript...
So here it goes, add this between the <head> tags of the pages you are having problems:
<base href='http://www.mydomain.com/'>
This will make that your relative path links will start with this base link. Simple as that.
The <base> tag specifies the base URL/target for all relative URLs in a document. Put the <base> tag as the first element inside the <head> element, so that other elements in the head section uses the information from the <base> element.
Did it work for you?
Consider rewriting only non-existing paths, e.g. if file or directory exists - don't rewrite it.
Wordpress uses this to rewrite their permalinks, I think it's pretty good example:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
As you can see they have rewrite conditions to exclude existing files/directories.
That .htaccess file is redirecting anything that doesn't include a / to index.php.
Consider rewriting it to catch only the paths you really need.
Alternatively, you could start your file with a rule indicated as "Last" (L):
RewriteRule ^/javascript.js$ $0 [L]
This would force Apache to evaluate this RewriteRule and then halt all other rules for that request. Thus, your .js or .css files would be served properly, but you'd have to make a new entry in the .htaccess for every such file. I'd strongly recommend my first suggestion over this one.
Try this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^([^/]*)$ index.php/?node=$1 [L]
Related
My gosh - I hate .htaccess ... and don't understand how the hell anything is working.
I am trying to ignore 'subfolders' and use these as guest identifyer.
Ex:
User types > http://subdomain.domain.com/guest_id
Server sends > http://subdomain.domain.com/index.php
Browser shows > http://subdomain.domain.com/guest_id
My htaccess file:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php [NC]
The code works great... unless there is a trailing slash.
In this case, all files are served as 'index.php'.
The main Index.php is showing properly, but all external scripts / css / etc. are served as /guest_id/index.php (thus... do not load).
FYI, there might be a conflict as my 'subdomain' is actually invisibly serving domain.com/event/
I have been trying so many permutations... without really understanding how htaccess work.
Your help will be sooooo much appreciated.
1,000 thanks in advance!
Damien
When you have URL such as http://subdomain.domain.com/guest_id/ and are using relative links for your css/js/images then browser attempts to resolve all links by prefixing them with current path hence src='image.png' is resolved as http://subdomain.domain.com/guest_id/image.png instead of http://subdomain.domain.com/image.png.
You can have a rule to force remove trailing slash at the end of all non-directory URLs.
RewriteEngine On
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.+)/+$
RewriteRule ^ %1 [R=301,NE,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
Also you can add this just below <head> tag of your page's HTML:
<base href="/" />
so that every relative URL is resolved from that base URL and not from the current page's URL.
I have a website running at localhost/pm and the RewriteBase is correctly set to /pm/. There is a link tag in my document: <link ... href="themes/default/css/default.css">.
When the url is localhost/pm or localhost/pm/foo the CSS works all right. When there are more slashes in the URL, however, like localhost/pm/foo/bar the relative URL if the stylesheet changes to foo/themes/default/css/default.css.
How do I get this to work without having to put some sort of PHP path resolution in the link tag?
# invoke rewrite engine
RewriteEngine On
RewriteBase /pm/
# Protect application and system files from being viewed
RewriteRule ^(?:system)\b.* index.php/$0 [L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php/$0 [PT]
EDIT:
Basically what I need now is this:
If request contains folder name /themes/ scrap everything that is before /themes/ and rewrite the rest to /pm/themes/...
I tried it like this: RewriteRule ^.*(/themes/.*)$ /pm/themes/$1 but I get an internal server error. Why?
If I do it like this: RewriteRule ^.*(/themes/.*)$ /pm/themes/ (ie. just remove $1 from the end) and use the URL http://localhost/pm/foo/themes/foo/ the resulting physical location is http://localhost/pm/themes which is what is expected too, which in turn means that at least my regex is correct. What am I missing?
The RewriteRule is almost correct
RewriteRule ^.*(/themes/.*)$ /pm/themes/$1
This rewrites http://localhost/pm/foo/themes/default/css/default.css to http://localhost/pm/themes/themes/default/css/default.css, which is one themes too much. Use this instead
RewriteRule /themes/(.*)$ /pm/themes/$1 [L]
But now you have an endless rewrite loop, because /pm/themes/.. is rewritten again and again. To prevent this, you need a RewriteCond excluding /pm/themes
RewriteCond %{REQUEST_URI} !^/pm/themes/
RewriteRule /themes/(.*)$ /pm/themes/$1 [L]
Now the request is rewritten only once and you're done.
You probably need to add the following lines before your RewriteRule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
It will only evaluate your rewrite rule if the requested file or directory doesn't exist.
You should post your .htaccess file so we can offer better advice
I want to to use flat links like this:
http://somedomain.com/about-us
http://somedomain.com/products-list
http://somedomain.com/product/item
Thus I've used the mod_rewrite rules like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9A-Za-z-]+)?$ /index.php?page=$1 [L]
RewriteRule ^([0-9A-Za-z-]+)/([0-9A-Za-z-]+)?$ /index.php?page=$1&value=$2 [L]
</IfModule>
The first two links are working fine. But whenever I visit the third type of links the images or the css or any js script that are linked as relative path eg. <img src="images/image.jpg"> and About Us.
The browser thinks it is in http://somedomain.com/product/images/image.jpg and http://somedomain.com/product/about-us.
The correct path should be http://somedomain.com/images/image.jpg and http://somedomain.com/about-us
And thus files with relative links are not working.
How to fix this issue? I don't want to use full path instead of relative for all linked files. Please suggest a way by doing some tweaks in the .htaccess
Try escaping your slash:
RewriteRule ^([0-9A-Za-z-]+)\/([0-9A-Za-z-]+)?$ /index.php?page=$1&value=$2 [L]
I have created a simple mod rewrite for my site:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/(cgi-bin|css|images|js|gfx)/?.*$ [NC]
RewriteRule ^filter/model/([^/]*)/mins/([^/]*)/texts/([^/]*)/freegifts/([^/]*)/network/([^/]*)/merchant/([^/]*)$ /fiverrr.php?model=$1&mins=$2&texts=$3&freegifts=$4&network=$5&merchant=$6 [L]
but it doesn't seem to be working (as in all the images/js etc don't show up)
e.g. http://mydomain.com/filter/model/Sony+Ericsson+Xperia+Arc+S+White/mins/+3000/texts/+-1/freegifts/FREE+Nintendo+3DS+Black/network/Orange/merchant/
original url: http://mydomain.com/fiverrr.php?model=Sony+Ericsson+Xperia+Arc+S+White&mins=+3000&texts=+-1&freegifts=FREE+Nintendo+3DS+Black&network=Orange&merchant=Contract
Where am I going wrong, and is this the best method?
Thanks so much
Try to put there another condition - htaccess will rewrite it only if requested file does not exist:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^filter/model/([^/]*)/mins/([^/]*)/texts/([^/]*)/freegifts/([^/]*)/network/([^/]*)/merchant/([^/]*)$ /fiverrr.php?model=$1&mins=$2&texts=$3&freegifts=$4&network=$5&merchant=$6 [L]
Also I recomend you to check full paths. Check if you have images in HTML code like: http://mydomain.com/images/img.jpg - relative paths like ./../images/img.jpg will not work!
In order to have nice clean urls with some kind of site archetecture I am using modrewrite in my .htaccess file. I don't want file extensions (.php or .html).
I have the following:
<IfModule mod_rewrite.c>
RewriteEngine On
</IfModule>
RewriteRule ^([a-zA-Z0-9-]+)/$ $1.html [L]
RewriteRule ^([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/$ $1/$2.html [L]
RewriteRule ^([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/$ $1/$2/$3.html [L]
But I want it to check if an index.html file exists in the sub-directory first.
For example, I want
http://example.com/first/
to rewrite to
http://example.com/first.html
but if that does not exist then rewrite to
http://example.com/first/index.html
The same for sub-directies, for example I want
http://example.com/first/second/
to rewrite to
http://example.com/first/second.html
but if that does not exist then rewrite to
http://example.com/first/second/index.html
Is this possible, and how do I achieve this?
This should work for you (works for me):
DirectoryIndex index.html
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Do not do anything for already existing files and folders
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .+ - [L]
# add .html file extension (if such file does exist)
RewriteCond %{DOCUMENT_ROOT}/$1\.html -f
RewriteRule ^(.+[^/])/?$ $1.html [L,QSA]
</IfModule>
If you are using <IfModule mod_rewrite.c> then place ALL rewrite rule into between the tags, not just RewriteEngine On.
The rule checks if such file with .html extension does exist before rewriting. For http://example.com/first/second/ it will check if WEBSITE_ROOT/first/second.html does exist. If not -- the will leave as is, and Apache will automatically pick up http://example.com/first/second/index.html because of DirectoryIndex index.html directive (see the very first line).
Please note: these 2 URLs are treated as the same from rewrite engine point of view: http://example.com/first/second/ and http://example.com/first/second (notice that there is no trailing slash). If you want this to be 2 different URLs (only rewrite one with trailing slash) then remove ? and [^/] from last rewrite rule (e.g. RewriteRule ^(.+)/$ $1.html [L,QSA]).
For URL http://example.com/first/second/ -- if you have both folder /first/second/ and file /first/second.html present, then request will go to a folder (and /first/second/index.html will be the final URL -- see #2).
You can use a RewriteCond something -f to check if something is actually a file. For directories, you can use the -d flag instead.