I have a rewrite rule currently as shown below:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ $2.php?locale=$1 [QSA,L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+) $2.php?page=$3&locale=$1 [QSA,L]
My page URLs are like this:
http://example.com/en/new
or this:
http://example.com/en/new/1
As for the index page it's like this:
http://example.com/en/index
and I wanna get rid of the 'index' word so I added this rule:
RewriteRule ^(.*)$ index\.php/$1 [L]
Which works as expected except for my web assets (css, js files) which are located under: /css and /js folders now has 500 error. So my question is how to I exclude URLs pointing to these files under these 2 directories from being rewritten.
This solved my problem. Either use the suggested answer from here
http://stackoverflow.com/questions/1848500/htaccess-mod-rewrite-how-to-exclude-directory-from-rewrite-rule?rq=1
or simply create a .htaccess file inside the directory where you don't want rewriting to take place and put this in:
RewriteEngine Off
Related
I'm not so good with htaccess and tried to find an answer to my question but no luck so far.
So I have this .htaccess rewrite:
RewriteCond %{REQUEST_URI} /(api|nova|nova-api)
RewriteRule .* /index.php
Which works well.
The website is an Angular site where I have dynamic URLs which are routed by JS.
So if I open base domain: example.com works well because index.html is served.
But if I open a route like: example.com/example-route. It says 404.
Could you please help me how should I modify the .htaccess file?
RewriteCond %{REQUEST_URI} /(api|nova|nova-api)
RewriteRule .* /index.php
You would seem to just need to rewrite the request to index.html after your API rewrite to index.php. However, you should modify your existing rule to add the L flag and the regex that matches the request should be anchored (although the condition is not required at all since the URL check should be performed in the RewriteRule directive itself).
For example, try the following instead:
# "index.html" needs to take priority when requesting the root directory
DirectoryIndex index.html
# Abort early if request is already "index.html" or "index.php"
RewriteRule ^index\.(html|php)$ - [L]
# Rewrite certain requests to Laravel API
RewriteRule ^(api|nova|nova-api)($|/) index.php [L]
# Rewrite everything else to Angular
# (unless it already maps to a file or directory)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.html [L]
Since the "homepage" is already working OK, it would suggest DirectoryIndex is already set OK in the server config (and prioritising index.html), although explicitly setting this to just index.html (as above) is more optimal, if this is all that's required.
I am building an admin section in codeigniter for my site and wanted to have a separate CSS folder for the CSS files for the admin section as they are going differ to those for the main site.
Current my CSS files sit in assets/css
I added and extra folder to that called admin and placed my new CSS files in that ie
assets/css/admin/css/style.css
However when I click the link in source code view to that stylesheet, I get a 404.
My current .htaccess reads as follows:
RewriteEngine On
RewriteCond $1 !^(index\.php|assets|editor|css\/|admin|js|scripts|images|img|media|xml|user_guide|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
What would I need to do to enable this file.
Note the strange thing is I have 4 CSS files in the folder, two of them whem clicked in the view source ope okay, and 2 of them contain the 404.
use this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
I have a default rule for my PHP framework in .htaccess file that routes all requests except specific files to index.php.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !\.(pdf|js|ico|gif|jpg|png|css|rar|zip|tar\.gz)$ index.php [L]
For example, if I have site-specific images in /images folder, request foo.com/images/name.jpg will be processed directly to filesystem.
But I need a special behavior for URLs with specific path, f.e. /image. So requests like foo.com/image/name.jpg should also be routed to index.php...
RewriteRule .*\.(pdf|js|ico|gif|jpg|png|css|rar|zip|tar|gz)$ /index.php [L]
I've got the following rules to work which:
only act on files that exist
exclude any files that contain images|js|css in their uri
add trailing slash to request uri
Rewrite rules:
RewriteEngine on
DirectorySlash Off
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/(images|js|css)$
RewriteRule ^(.*[^/.])$ /$1/ [R=301,L]
I now need to correctly redirect my home uri's like so:
http://www.example.com/sitemap/ -> http://www.example.com/index.php?page=sitemap
I've tried the following approach:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/.])$ index.php?page=$1 [R=301,L,NC]
But I get a page not found, presumably because $1 is being fed something with a slash in it. I thought [^/] would remove it but apparently not.
Could someone explain where I am going wrong here please?
Use this rule -- it will rewrite /sitemap/ into /index.php?page=sitemap:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ /index.php?page=$1 [QSA,L]
Put it into .htaccess into website root folder. If placed elsewhere it need to be tweaked a bit.
URL will stay the same. Existing query string will be preserved.
The trailing slash / must be present (i.e. /sitemap will not trigger this rule).
It will only rewrite if there is no such folder or file (i.e. if you have a folder named sitemap in your website root folder then no rewrite will occur).
It will only work for 1-folder deep URLs (e.g. /sitemap/, /help/, /user-account/ etc). It will not work for 2 or more folders in path (e.g. /account/history/).
RE: this line: RewriteCond %{REQUEST_URI} !/(images|js|css)$.
You said you want "exclude any files that contain images|js|css in their uri". Unfortunately the above pattern work differently -- it will match /something/css but will not match /css/something or /something/file.css.
If you want to match images|js|css ANYWHERE in URL straight after a slash, then remove $.
I am trying to rewrite all requests to a file that doesn't exist but the folder does.
What I mean is:
Say I have this folder structure:
foo
'-bar1
'-bar2
'-bar2.html
'-shared
'-shared.html
What I am looking for the rewrite rule to do is to serve up example.com/foo/bar2/bar2.html as normal. Serve example.com/foo/bar1/bar1.html as /foo/shared/shared.html and to no serve example.com/foo/bar3/bar3.html.
So in summary. I am trying to develop a RewriteCond that hits only when the directory exists but the file doesnt exist in that directory.
The problem with:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
is that it will rewrite www.example.com/bar3/bar3.html even though /foo/bar3 directory doesn't exist.
Thanks for the help!
These rules should do the job:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1/ -d
RewriteRule ^(.+)/([^/]+)$ /foo/shared/shared.html [L,QSA]
These rules intended to be placed in .htaccess file in root folder. If you need to place them in config file (e.g. httpd-vhost.conf) then you will need to modify it a bit (remove some slashes in 2nd RewriteCond .. or add leading slash in RewriteRule -- testing is actually required).
These rules will only work if requested resource has at least 1 folder (obvious -- as requested).