.htaccess: Serve static files, route everything else to index.php - apache

I want to use an .htaccess file to check if the requested path is a file in the public/ directory. If yes, serve it, else forward request to /index.php. I can't seem to get this to work.
Here's what I've got:
Options +FollowSymLinks
RewriteEngine on
Options -Indexes
RewriteCond %{DOCUMENT_ROOT}/public%{REQUEST_URI} -f
RewriteRule ^ %{DOCUMENT_ROOT}/public%{REQUEST_URI} [L]
RewriteRule ^ index.php [QSA,L]
e.g. http://example.com/css/style.css should have apache serve /public/css/style.css because it's a file that exists, but http://example.com/css/style.bad should be sent to /index.php.

This code should be working as expected on your side
Options +FollowSymLinks -MultiViews -Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{DOCUMENT_ROOT}/public/$1 -f
RewriteRule ^(.*)$ public/$1 [L]
RewriteCond %{THE_REQUEST} \s/public/ [NC,OR]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L,QSA]

Apparently [L] does not work as I expected. With that in mind, and a lot of trial and error, I managed to find something that works:
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/public%{REQUEST_URI} -f
RewriteRule ^ public%{REQUEST_URI} [L]
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^ index.php [QSA,L]

Related

Need help to fix my .htaccess file

My index.php file is located at:
myserverHost/api/public/
which include api calls like getting token. like:
myserverHost/api/public/token
It works only if i type exact URL. i.e.
myserverHost/api/public/token
i am trying to write rewrite rule where i can use:
myserverHost/api/token
here what i have tried in my .htaccess file content:
RewriteEngine On
#RewriteBase /api/
RewriteRule .* - [env=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
# Make sure $HTTP_RAW_POST_DATA is deprecated warning does not appear
#php_value always_populate_raw_post_data -1
Try this, this will remove public in format public/x -> X
RewriteEngine On
#RewriteBase /api/
RewriteRule .* - [env=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ public/$1 [L]
RewriteRule ^ index.php [QSA,L]

htaccess issue causing styles and images not to load

For some reason stylesheets and images are not loading on my site :(
Don't really know why I believe it may be to do with my htaccess file here is how it currently is.
UPDATE - NEW FILE
#SetEnv APPLICATION_ENV development
DirectoryIndex /public/index.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.+) $1 [L]
RewriteCond %{DOCUMENT_ROOT}/public%{REQUEST_URI} -f
RewriteRule ^(.+) /public/$1 [L]
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /public/index.php?q=$1 [L,QSA]
RewriteRule !\.(js|ico|gif|jpg|png|css|xml|xslt)$ /public/index.php
Any idea why its not loading?
It looks like there has been some cut-and-pasting: 'RewriteEngine on' should be there only once and the code from that point is causing you trouble.
The reasons for those lines are not clear without more info on the kind of site you run, so any advice from here is trial and error testing:
The last rule is commented out and looks like an effort to get the stylesheet and images (all js|ico|gif|jpg|png|css|xml|xslt files) correctly rewritten to the desired address. You can try to comment it out (get rid of the #).
The problem was fixed by rewriting the whole file to the following
DirectoryIndex /public/index.php
RewriteEngine On
Options +FollowSymLinks -MultiViews -Indexes
RewriteBase /public
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(domainUrlNoHttp)$ [NC,OR]
RewriteCond %{HTTP_HOST} ^http://(domainUrl)$
RewriteCond %{REQUEST_URI} !public/
RewriteRule (.*) /public/$1 [L]

htaccess is supposed to remove index.php from current URL, but instead redirects to root

I've read been reading StackOverflow posts for the last 30 minutes and none of them work. Maybe there is a server setup that is preventing this from working?
I just want to remove index.php from whatever URL is typed in. For example, www.mysite.com/blah/blah/index.php would become www.mysite.com/blah/blah/. www.mysite.com/index.php would become www.mysite.com/.
I've read at least 10 posts and tried each one, but it ALWAYS just redirects to the root. Here is the current code I'm using that looks like it should work:
Options +FollowSymLinks
Options +Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
</IfModule>
Instead of just removing index.php from the URL, it redirects to the root of the site.
I think this is what you are looking for:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ $1/index.php [L,QSA]
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.php($|\ |\?)
RewriteRule ^ /%1 [R=301,L]
</IfModule>

Change page url in htaccess

I have my own CMS system, but I can't make a code in .htaccess file.
I want redirect from http://example.com/?page=forum to http://example.com/forum...
I don't know, how do it...
I tried the following:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /projects/cms/
RewriteCond ?page=([^\s&]+) [NC]
RewriteRule ^ %1? [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
But it didn't work.
Sorry for my bad English, thanks for Code.
The comment splash58 posted is correct - you need to check the QUERY_STRING for the presence of page=<something>, like this:
RewriteCond %{QUERY_STRING} page=([^\s&]+) [NC]
RewriteRule ^ %1? [R=301,L,NE]
Your file should now look like this:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /projects/cms/
RewriteCond %{QUERY_STRING} page=([^\s&]+) [NC]
RewriteRule ^ %1? [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]
If it is still not working, then you need to ensure that mod_rewrite is enabled on your server, and that AllowOverride All is set in your server or virtual host configuration. See this for more information on how to do so: http://tildemark.com/enable-htaccess-on-apache/

Few rules in my htaccess file is not working

I am facing some issues with my htaccess. Below is htaccess file I am using. The 301 redirect is working fine but redirects for books isnt working at all.
Another rule at bottom of file ^books.php /books/ [R=301,L] is also working. I am just wondering what is wrong with other rules.
Website is www.gatecounsellor.com
http://www.gatecounsellor.com/books.php is getting redirected according to rule but not this, http://www.gatecounsellor.com/books/aerospace-engineering-ae/
RewriteOptions inherit
Options +FollowSymlinks
Options -Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^gatecounsellor\.com$ [NC]
RewriteRule ^(.*)$ http://www.gatecounsellor.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^books/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ /books/display_book_detail.php?branch=$1&subject=$2&title=$3&isbn_13=$4 [NC,L] # Handle product requests
RewriteRule ^books/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ /books/display_subject_books.php?branch=$1&subject=$2 [NC,L] # Handle product requests
RewriteRule ^books/([A-Za-z0-9-]+)/?$ /books/display_branch_subjects.php?branch=$1 [NC,L] # Handle product requests
RewriteRule ^books.php /books/ [R=301,L]
AddDefaultCharset UTF-8
Any clues what could be wrong in other rules?
Thanks
It could be due to enabling of MultiViews:
Place this line on top of your .htaccess:
Options -MultiViews
UPDATE: Move these 2 rules in /books/.htaccess:
Options +FollowSymlinks -Indexes -MultiViews
RewriteEngine On
RewriteBase /books/
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ display_book_detail.php?branch=$1&subject=$2&title=$3&isbn_13=$4 [NC,L]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ display_subject_books.php?branch=$1&subject=$2 [NC,L]
RewriteRule ^([A-Za-z0-9-]+)/?$ display_branch_subjects.php?branch=$1 [NC,L]