mod_rewrite not working correctly - accessing static resources - php

Right now I have a few folders (img, css, js, and ico) in the root directory of my website. However, I want to move them into a new directory called public_html. For example, instead of the image folder being located in /img, it would be instead located in /public_html/img. But I'm having problems doing this, and i suspect it's a problem with my .htaccess file.
Options -Indexes
Options +FollowSymLinks
RewriteEngine On
ErrorDocument 404 static/404.html
ErrorDocument 403 static/403.html
RewriteCond %{REQUEST_FILENAME} !-f
# ---------- Custom Routes ----------------
# -----------------------------------------
RewriteRule ^(js|css|img|ico)\/(.+)$ public_html/$1/$2
RewriteRule ^(.*)$ index.php?r=$1 [L,QSA]
I can access /public_html/css/style.css just fine, but when I add in that first RewriteRule line and try to access /css/style.css, it doesn't work.
Can anyone figure out why? Thanks.

Add the [L] flag to the first rule. Otherwise it drops through to the second rule and attempts to pass it in r= to index.php. Accessing it via public_html works because of the RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(js|css|img|ico)\/(.+)$ public_html/$1/$2 [L]
# Update: Try using the RewriteCond before this line
# as well as where you have it earlier
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?r=$1 [L,QSA]

Related

Remove Subfolder[Codeigniter] only for specified URLs using Htaccess

My Folder Structure
Root Domain
assets/ (contains css, images, js folders)
subfolder/ (contains Codeigniter Project inside and seperate .htaccess)
index.php
contact.php
.htaccess
Root Htaccess
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !(.*)subfolder
RewriteRule ^(.*)$ subfolder/$1 [L]
Subfolder(Codeigniter Project) Htaccess
DirectoryIndex index.php index.html
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond $1 !^(index\.php|assets|install|update)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# For godady Shared Hosting Server uncomment the line below
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
List of Subfolder URLs
http://example.com/subfolder/products/
http://example.com/subfolder/product/product-slug-123
List of Root URLs
http://example.com/index.php
http://example.com/contact.php
In my Codeigniter Project I've set Products controller as default_controller.
So the problem is, when I try to access the index.php page in the Root folder it only displays the Codeigniter products list as Home page.
I need following URLs:
http://example.com/index.php
http://example.com/contact.php
http://example.com/products/
http://example.com/product/product-slug-123
Any solutions with htaccess or with code?
Finally I got this working.
RewriteCond %{REQUEST_URI} !((.*)subfolder|^/assets) // to Exclude this
RewriteCond %{REQUEST_URI} (^/products(.*)|^/product(.*)) // to Include this
RewriteRule ^(.*)$ subfolder/$1 [L]
Exclamation(!) symbol used to 'not' matching urls.
So I removed Exclamation symbol and used another RewriteCond (Condition) to redirect only if the url contains segment as products or product http://example.com/products/ or http://example.com/product/product-slug-123. It's working perfectly now. I thought, sharing this would be useful to someone. Happy coding.

How to redirect all requests within a subfolder

I have a website called mydumbwebsite.com/. In my root folder, I have various folders, one of them being "stuff". This folder is directly accessible with mydumbwebsite.com/stuff/. I want to create an .htaccess file that redirects everything that goes into this subfolder and only this subfolder, so these urls:
mydumbwebsite.com/stuff/test
mydumbwebsite.com/stuff/test/
mydumbwebsite.com/stuff/test.php
mydumbwebsite.com/stuff/test.php?test=yes
mydumbwebsite.com/stuff/test2.php (doesn't exist)
mydumbwebsite.com/stuff/test2.php?test=yes (doesn't exist)
mydumbwebsite.com/stuff/test/moar
mydumbwebsite.com/stuff/test/moar/tests.php
... should all redirect to the index.php file of the folder "stuff", even if the file/folder they point to exists.
Some additional context: despite my made up example, I encountered this problem on localhost. I have many different projects and I don't want the .htaccess of one project interfere with the other projects. I tried this, but it keeps redirecting me to the xampp homepage:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php [QSA,L]
I tried changing it to this, but that couldn't cover all instances, such as nr. 2, 3 and 4:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /stuff/index.php [QSA,L]
You need to remove the conditions !-d and !-f, which say "Don't apply the rule on existing files and directories". You also need to remove RewriteBase /.
RewriteEngine On
RewriteRule ^(.+)$ index.php [QSA,L]
The file should be located under /suff/.htaccess.
The following rules do also work for me:
RewriteEngine On
RewriteRule ^ index.php [L]
and
RewriteEngine On
RewriteRule ^ index.php [END]
Regarding your comment:
Usually I would create a public folder and put anything that should be accessed directly (public/css/, public/js, public/img etc.) in it. And whitelist it as following:
RewriteEngine On
RewriteRule ^public/ - [END] # allow direct access on public folder
RewriteRule ^ index.php [END] # anything else will be directed to index.php
You can of course whitelist multiple directories the same way:
RewriteEngine On
RewriteRule ^css/ - [END]
RewriteRule ^img/ - [END]
RewriteRule ^js/ - [END]
RewriteRule ^ index.php [END]
Or something like you suggested in the comment:
RewriteRule ^(scripts|styles|morefoldernames)($|/) - [L]
It is late, so my brain could be muddled about this, but surely you simply want everything in stuff to be redirected to index.php, no if's or buts.
Then the below should work, provided it has "/stuff/" in the URL. I'm guessing that you aren't bothered about query strings etc either, if so, then you'll need to modify the index.php below to index.php?$1 and the flags to [QSA, NC, L].
RewriteEngine On
RewriteRule ^/stuff/?$ /stuff/index.php [NC,L]
Check that your root .htaccess file has the line "RewriteOptions InheritDown" somewhere in it, that way the .htaccess file for each subfolder should be covered.
Try this out.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/stuff/(.+)$ /stuff/index.php [L]

Rewrite URL not working in CodeIgniter

I have learned and make website using CodeIgniter. I want to create this system module base so I have created multiple folders and controller,views and model for each module and it's working fine. I want to remove index.php and hide query string like pass user id in URL. I have tried htaccess code which given user guide but it did not work.
Current URL :
mysite/sitename/index.php/departments/departments/delete_department/14
Require :
mysite/sitename/departments/departments
I have tried below code in htaccess but it does not work as expected, it displays page not found error.
RewriteEngine on
RewriteCond $1 !^(index\.php|images|fonts|js|css|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
My directory structure is something like that
My site
|_application
| |_ Controller
| |_Login (folder) // module folder
|_Models
| |_pages
|
|_views
|_login (folder) // module folder
1) you should have Rewrite mod on apache
2) You should change application/config/config.php
$config['index_page'] = 'index.php'; to: $config['index_page'] = '';
3) after that I see that you are having a subdirectory of sitename so you should add that on your .htaccess as your RewriteBase which will result in an .htaccess like this:
RewriteEngine On
RewriteBase /sitename/
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
If i understand your folder structure right, then your root folder is mysite and your CI application is located at mysite/sitename . if this is the case try checking out the last part of the rewrite
RewriteRule ^(.*)$ /index.php/$1 [L]
As it currently tries to load an index file that is located on your root folder (mysite) and under my understanding your index file is actually located on mysite/sitename, so you can try this:
RewriteRule ^(.*)$ /mysite/index.php/$1 [L]
Using your FTP client, create a new file named .htaccess (including the leading dot) in the same folder as your site’s main index.php file.
Then add the following code to this newly created .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
If your site’s system directory (/system/) has been renamed and is still accessible by URL, modify the RewriteCond line above:
RewriteCond %{REQUEST_URI} !/newdirectoryname/.* [NC]
If you are running EE from a sub-directory rather from the root of your domain (e.g. http://example.com/myeesite/ instead of http://example.com/), just remove the slash preceding index.php in the RewriteRule line above, like so:
RewriteRule ^(.*)$ index.php/$1 [L]
If you are running EE from a sub-directory and it still doesn’t work after removing the slash, you may need to specify the sub-directory in your rewrite rule. For example, if your sub-folder is named testing, change:
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
To:
RewriteRule (.*?)index\.php/*(.*) testing/$1$2 [R=301,NE,L]
And change:
RewriteRule ^(.*)$ /index.php/$1 [L]
To:
RewriteRule ^(.*)$ testing/index.php/$1 [L]
If your host requires forcing query strings, try adding a question mark following index.php in the RewriteRule line above, like so:
RewriteRule ^(.*)$ /index.php?/$1 [L]
do you have apache rewrite-mod enable ?
you must have apache rewrite-mod enabled to do that ..
Check your config at apache configuration path ..
Try this
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
change $config['index_page'] = 'index.php'
to
$config['index_page'] = ''
from
application\config\config.php
Also check your apache config for mod rewrite is enabled or not.
Checking URl Rewrite is Working or Not
MOD Rewrite
$isTrue = in_array('mod_rewrite', apache_get_modules());
Unfortunately, you're most likely trying to do this with CGI, which makes it a little bit more difficult.
You can test it using the following, though
$isTrue = strpos(shell_exec('/usr/local/apache/bin/apachectl -l'), 'mod_rewrite') !== false
If the above evaulates to true, then mod_write is enabled.
You are using the rewrite engine of apache. Make sure mod_rewrite is enabled and make sure your regular expression fulfills your needs.
Read about mod_rewrite here.
The htaccess rules look OK. Because you havent shown your code its hard to say but its either one of the following:
Your having an issue with your controller and method both being called 'departments'. If this is true either try renaming the method to something other than 'departments' or getting rid of it all together.
You dont have a controller called 'departments' (it doesnt show in your directory structure) or a route to handle the URI 'departments'. Either create the controller or add a route to direct the URI 'departments' to the controller you want to use. ie:
$route['departments/departments']='an_existing_controller/an_existing_method';
You dont have a method called 'departments'. If this is true get rid of the additional URI component called 'departments' or create a new method (as above dont call your method the same thing as your controller).
In config.php (which is mostly loated in application/config/config.php
make index page value to empty string, like this,
$config['index_page'] = '';
In root folder of your application make a .htaccess file with following code,
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
</IfModule>

htaccess redirects unexpectedly to folder

I have strange for me problem. I'm developing Zend Framework application and have this file structure:
/
/application/
/public/
/public/.htaccess
/public/index.php
/public/js/
/public/style/
/public/profile/
/.htaccess
My domain point to folder /
When I enter the address example.com/profile/ it goes to the controller profile and this is good.
When I enter the address example.com/profile the server redirects me to:
example.com/public/profile/
I would like to have a solution that whenever I request:
example.com/profile/ or
example.com/profile
The same page will be rendered but second version gives me a redirect and I don't know why.
The /.htaccess file is:
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
The role of this file is to route all traffic from / to /public but without any redirects.
The /public/.htaccess file is:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
Can anybody help me with this? Thanks.
I fixed this. Here is the solution if somebody have the same problem:
To fix this you have to set the following options and disable DirectorySlash
Options -Indexes FollowSymLinks
DirectorySlash Off
Now Apache shouldn't add trailing slashes at the end of uri when you pointing to directory.
This also disable redirect to the uri with trailing slash.
Optionally match the last /. Change your .htaccess file to this:
RewriteEngine On
RewriteRule ^(.*)/?$ public/$1 [L]
This works for me:
RewriteEngine On
RewriteRule ^/$|^(.*)$ /public/$1 [QSA,NC,L]

Tricky .htaccess and Drupal problem

1) I have a Drupal site located at http://example.com and I have a directory located at http://example.com/foo, but I also have a Drupal page with an alias of http://example.com/foo. How can I get the Drupal page to serve? Currently, I get a 403 forbidden page as a result of the Options -Indexes declaration in Drupal's .htaccess file, but I do not want to remove this as I do not want directories to be browsable.
EDIT: I have solved this with the following rule:
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ http://localhost/s2k/index.php?q=$1 [L,QSA]
2) To make the problem even more difficult, given the same scenario, if I have an index.html file inside the directory http://example.com/foo/index.html I always want this to take priority over Drupal's aliased page (which it does at the moment - currently I have modified the .htaccess file so that any directory with an index.html file displays it - DirectoryIndex index.php index.html).
EDIT:
So now, how can I write a RewriteCond that will look to see whether or not there is an index.html file inside the directory?
I am no RewriteRule Guru and pretty sure there is a nicer way to achieve this, e.g. with conditional RewriteCond.
But simply adding
RewriteCond %{REQUEST_URI} =/foo/
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
Above the default rewrite Conds and rules make this rule kick in for /foo/ and not the Drupal-default one. Where a RewriteCond %{REQUEST_FILENAME} !-d will only apply the RewriteRule if something is NOT a dir.
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteRule ^(.*)$ http://localhost/s2k/index.php?q=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME}index.html -f
RewriteRule ^(.*)$ $1index.html [L]
RewriteCond %{REQUEST_FILENAME}/index.html -f
RewriteRule ^(.*)$ $1/index.html [L]
[normal Drupal rewrite rules]

Categories