I have some magic stuff in my htaccess. It works very well but I don't know how
RewriteRule ^[^_+/]. index.php
I think it says that all requests should go through index.php no matter which directory the visitor ask for. So far so good. However images, css files, js files etc should of course not be parsed in index.php. Can I exclude certain directories from the above rule? Like /img, /css and /js
Other suggestions?
Thanks in advance
you can exclude existing files and directories
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
by placing this before your rule
You can avoid requests to certain paths by using a simple RewriteCond...
RewriteCond ${REQUEST_URI} !^css
RewriteCond %{REQUEST_URI} !^img
RewriteCond %{REQUEST_URI} !^js
# or alternatively just
RewriteCond ${REQUEST_URI} !^(css|img|js)
Related
I have checked a large amount of existing answers regarding .htaccess redirects. However none of them have helped me.
What I want to accomplish is redirecting all request urls to /api/init.php. However I've only gotten so far to where my index page www.example.com simply gives me a file listing because of the missing index.php file, while every url request with a path is working.
How can I accomplish this with .htaccess without ending up with a directory listing on my landing page?
This is as far as I got:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /api/init.php?path=$1 [NC,L,QSA]
Well your site root is a directory, so this rule you have excludes existing directories. What you could do is only exclude existing files, and allow existing directories to be handled by the PHP script. Like this:
RewriteEngine on
RewriteCond %{REQUEST_URI} !=/api/init.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /api/init.php?path=$1 [L,QSA]
I removed the NC flag as it's not needed. I added a condition to prevent an unnecessary file-system check.
You don't have to pass the path on in a URL parameter, as you could get it from $_SERVER['REQUEST_URI'] in PHP (not the same as REQUEST_URI in mod_rewrite, in PHP it always has the original URI). If you wanted to do that then your rule becomes nice and simple:
RewriteEngine on
RewriteCond %{REQUEST_URI} !=/api/init.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /api/init.php [L]
Because the query string will just be passed on unaffected (so QSA is not needed).
I am trying to make .htaccess rule not affect other file url
example
my .htaccess rule is
RewriteEngine On
RewriteRule ^([^/]*)$ /tr/hp.php?q=$1 [L]
my site url is
mydomain.com/keywords
everything working good on keywords but when I try to open robots.txt
mydomain.com/robots.txt
OR
mydomain.com/images.jpg
any other file url
redirect on /tr/hp.php?q=filename
which .htaccess Rewrite Rule works on both?
Try :
RewriteEngine On
#--exclude real directories
RewriteCond %{REQUEST_FILENAME} !-d
#--and files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ /tr/hp.php?q=$1 [L]
You also have to prevent matching any request pattern that carries a dot in it:
RewriteEngine On
RewriteRule ^([^/.]*)$ /tr/hp.php?q=$1 [L]
Certainly it is possible to further refine this pattern.
Alternatively some people like to prevent rewriting requests to files or folders that physically exist in the file system using RewriteCond:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
But that is something you have to decide upon. This does not help for example if resources like robots.txt are delivered in a dynamic manner...
Here is the file structure of my web
css/
script/
images/
.htaccess
index.php
ajaxusername.php
.htaccess content
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(\w+)/(\w+)$ index.php?d=$1&t=$2 [QSA]
RewriteRule ^(\w+)/?$ index.php?d=$1
When input http://www.abc.com/peter/, it can translate into http://www.abc.com/index.php?d=peter successfully.
When i input http://www.abc.com/peter/password, it can translate into http://www.abc.com/index.php?d=peter&t=password successfully too.
The problem is:
/peter/ can get the resources listed in above folders successfully but,
/peter/password cannot get those resources.
When I checked in firebug, it shows me it points to http://www.abc.com/peter/css/user.css, which is an invalid URL.
The correct URL must point to http://www.abc.com/css/user.css instead the previous one.
Any suggestion? Thanks!!
Problem is that you're using relative paths for css, js and images and once you start using pretty URL scheme of /part1/part2 it messes up URLs of your resources. As you've noted that css URI becomes /peter/css/user.css instead of /css/user.css.
Here are some solutions for you to help.
Solution 1: To overcome your issue I recommend using 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 /.
Solution 2: It is no doubt better to use absolute path for static files (css, js, images etc). But if you have lots of those instances in several pages then consider using HTML base tag to specify a default URL for relative paths. eg:
<base href="http://www.abc.com/" />
Solution 3: mod_rewrite based solution. Use it when you can't implement above solutions.
Have you .htaccess like this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# don't do anything for a real directory, file or link
RewriteCond %{REQUEST_FILENAME} !-d [OR]
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule - [L]
# your original rewrite rules
RewriteRule ^(\w+)/?$ /index.php?d=$1 [QSA,L]
RewriteRule ^(\w+)/(\w+)/?$ /index.php?d=$1&t=$2 [QSA,L]
# rules to fix wrong paths of css, js and images
RewriteRule ^[^/]+/((?:css|script|images)/.*)$ /$1 [L,R=301,NC]
It looks like you need to add exceptions to your rewrite rules for requests to files and directories that exist on the server, otherwise when the files are requests by the client's browser they won't be served since the url is rewritten.
Try adding these lines:
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .? - [S=2]
into your htaccess file:
Options +FollowSymlinks
RewriteEngine on
...put them here...
RewriteRule ^(\w+)/(\w+)$ index.php?d=$1&t=$2 [QSA]
RewriteRule ^(\w+)/?$ index.php?d=$1
And your resources should be accessible.
I am trying to use mod_rewrite on a Ubuntu 12.04 server to make my URLs more readable, however I want to add an exception for images and css files.
My input URLs are in the format \controller\action which is then re-written to index.php?controller=controller&action=action. I want to add an exception so that if an image or css file is specified, the URL is not re-written, e.g. \images\image.jpg would not be re-written.
My .htaccess code is as follows:
RewriteEngine on
RewriteCond %{REQUEST_URI} !(\.gif|\.jpg|\.png|\.css)$ [NC]
RewriteRule ^([a-zA-z]+)/([a-zA-z]+)$ test.php?controller=$1&action=$2 [L]
RewriteRule ^([a-zA-z]+)/([a-zA-z]+)/([^/]*)$ test.php?controller=$1&action=$2&$3 [L]
My re-write code is working fine and the URLs are coming out as intended, however even if I request an image, the URL is still being re-written. It appears that my RewriteCond is being ignored, anyone any suggestions as to why this might be?
The RewriteCond only applies to your first RewriteRule, it should be reproduced for the second rule. However, I think that is better to add a non-rewriting rule, before, to exclude existing stuffs.
# Do nothing for files which physically exist
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
# your MVC rules
RewriteRule ^([a-zA-z]+)/([a-zA-z]+)$ test.php?controller=$1&action=$2 [L]
RewriteRule ^([a-zA-z]+)/([a-zA-z]+)/([^/]*)$ test.php?controller=$1&action=$2&$3 [L]
The rewriteCond rule is only applied for the next RewriteRule.
So you need to at least repeat the rewriteCond for your seconde RewriteRule.
No there is certainly better things to do.
For example a usual way of doing it is to test that the url is matching a real static ressource. If all your php code is outside the web directory (in libraries directory, except for index.php) then all styatic ressources available directly on the the document root can only be js files, css files, or image files.
So this is the usual way of doing it:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-zA-z]+)/([a-zA-z]+)$ test.php?controller=$1&action=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-zA-z]+)/([a-zA-z]+)/([^/]*)$ test.php?controller=$1&action=$2&$3 [L]
But this is a starting point. We could certainly find something to avoid doing 2 rules for this (maybe I'll have a look later)
I tried to search a lot but got no success. I wish that someone could really help me out with this.
Ok, so my website is like:
/rewrite.php
/folder1/rewrite.php
/folder2/rewrite.php
/folder1/subfolder1/rewrite.php
/folder1/subfolder2/rewrite.php
and so on...
In short, there exist a rewrite.php in each and every folder(except admin access folder).
So basically, I want to rewrite to the rewrite.php file in the corresponding directory, but I am not able to do it.
That is
If I request
/News
then it should rewrite it to:
/rewrite.php
If I request:
/folder1/News
then it should rewrite it to:
/folder1/rewrite.php
and so on for other folders.
Currently, this is what I have in my htaccess:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . rewrite.php [L,NC]
Somewhere, I read that If I remove RewriteBase, then it uses the current directory as RewriteBase. But it failed and didn't worked.
Also, I would like to keep some folders in exceptions(in which Rewriting should be disabled) like:
/administration
/config
I hope for a feasible solution, if anyone can help me... :)
Best Regards,
Ankur Thakur
Try:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/(administration|config|rewrite.php)
RewriteRule ^(.*?)(/?[^/]+)$ $1/rewrite.php [L]