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]
Related
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]
I'm working on an MVC project and I have the following .htaccess file:
Options -Indexes
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?path=$1 [L]
RewriteRule !^(public/|index\.php) [NC,F]
It works OK. I only want the public/ folder and the index.php file to be accessible to the public. All other paths should be inserted into the path GET parameter. For example, mysite.com/controller/method should point to mysite.com/index.php?path=controller/method.
Now, there is a problem. When visiting the URL directly (without including index.php, it is adding [NC,F] to the GET path parameter. It's like visiting mysite.com is pointing to mysite.com/index.php?path=[NC,F].
Why is this happening and how do I fix it?
EDIT
I moved index.php into the public/ folder. Here is my .htaccess file now:
Options -Indexes
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ public/index.php?path=$1 [L]
RewriteRule ^(/)?$ public/index.php [L]
RewriteRule !^(public/) [NC,F]
It seems to work OK. Are there any other improvements I could make on this?
You don't have a redirect location on the last rule, so it's taking the flags as the redirect location. Just a dash will be fine since it's a forbidden response. Change the last line to:
RewriteRule !^(public/|index\.php$) - [NC,F]
Also adding the dollar sign after index.php just to be clear.
Edit:
I would suggest updating your new rule set to the following (actually I suggest a complete re-think below, but this is an update on what you have):
RewriteEngine On
RewriteRule ^$ public/index.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ public/index.php?path=$1 [L]
RewriteRule !^(public/) - [NC,F]
The (/)? wasn't needed in your homepage rule, as the opening forward slash is not included in .htaccess matches anyway.
I moved your rule for the homepage to the top or it will never be used due to being matched by the previous rule (so the path param is not there when empty, which is presumably what you intended).
I stopped anything in /public/ from being passed to your index.php script, since the way you had it, anything in public that didn't exist would have been passed to your index script, which does not seem to be what you intend.
I added RewriteCond %{REQUEST_URI} !=/public/index.php so the rule couldn't be executed on itself and create a loop if rule processing is run through more than once, which it can be, but then took it back out because the above match on /public/ covers that anyway.
A Re-Think
All that said, I don't think it really makes sense to check if files don't exist and then just send forbidden responses to the ones that do, yet send everything else to your index script. Why not just send everything to your index script? That seems to be what you want really. I would suggest you simplify to this:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ public/index.php?path=$1 [L]
Dropping the homepage rule since no need to worry about an empty path parameter being passed to your index script. Changing the logic to be "Leave anything in /public/ alone. For anything else, pass it through to the index.php script." so files tests not needed since the script handles it all, and no forbidden response needed because there is nothing left to match, it's all covered by the rules. You can always return forbidden to anything you don't want to process in your script, which you would have needed to do anyway for existing file URLs in your previous setup.
One Last Re-Think
And finally, if I might suggest, it would be cleaner to have your index.php file in the root of the website, so you can make /public/ work with its own index file later if you like, so finally I would move it back to the root and change the rules to this:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/public/
RewriteCond %{REQUEST_URI} !=/index.php
RewriteRule ^(.*)$ index.php?path=$1 [L]
And if you like all that, an up-vote to go with already accepting the answer would be much appreciated. :)
Adding RewriteRule ^(/)?$ public/index.php [L] seems to have resolved the issue. I'm not sure if this is the best approach, but here is my .htaccess file now:
Options -Indexes
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ public/index.php?path=$1 [L]
RewriteRule ^(/)?$ public/index.php [L]
RewriteRule !^(public/) [NC,F]
I moved index.php into the public folder to make things clearer.
I have a sub folder with php files.
Like this: domain.com/subfolder/file.php
It would be very useful to call the php files as if they were on the root folder, without ignoring the existing root files.
Is there a way to include a subfolder and all its contents through .htaccess?
You can use the following rule in root/.htaccess :
RewriteEngine on
#1--If the request is not for an existent root directory--#
RewriteCond %{REQUEST_FILENAME} !-d
#2--And the request is not for an existent root file--#
RewriteCond %{REQUEST_FILENAME} !-d
#3--Then, rewrite the request to "/subfolder"--#
RewriteRule ^([^/]+)/?$ /subfolder/$1 [NC,L]
The RewriteConditions above are important to avoid rewriting your root folder and files to /subfolder.
Or try this :
RewriteEngine on
#--if /document_root/subfolder/foo is an existent dir--#
RewriteCond %{DOCUMENT_ROOT}/subfolder/$1 -d [OR]
#--OR /document_root/subfolder/foo is an existent file--#
RewriteCond %{DOCUMENT_ROOT}/subfolder/$1 -f
#--rewrite "/foo" to "/subfolder/foo--#
RewriteRule ^(.+)$ /subfolder/$1 [NC,L]
How about something like this?
RewriteRule ^subfolder/(.*)$ $1
It should help I think
P.S please make sure that apache rewrite module is enabled and turned of in .htaccess file
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>
i need to hide the extensions of my webpage and also want to let the user put the links in both (lower and upper) case:
Example:
the file name is demo.php
www.example.com/demo
www.example.com/DEMO
www.example.com/Demo
Running PHP in a LAMP server, no access to php.ini, just .htaccess
Actualy im using a file like this:
RewriteEngine On
RewriteBase /
RewriteRule ^/(OUTSOURCING|outsourcing|Outsourcing)$ outsourcing.php [NC,L]
And i m reciving this error:
Not Found
The requested URL /outsourcing was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
RewriteEngine On
RewriteBase /
www.example.com/DEMO www.example.com/Demo or doing www.example.com/DEMO/page2
RewriteRule ^/(DEMO|demo|Demo)/(.*)$ demo.php?=$1 [NC,L]
RewriteRule ^/(DEMO|demo|Demo)$ demo.php [NC,L]
RewriteRule ^/(D|d)emo$ demo.php [NC,L]
or pass anything www.example.com/DeMo www.example.com/bob to demo.php
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ demo.php [NC,L]
you may want to test if your allowed .htaccess RewriteRule /*$ http://google.com [R][L]
here is a good way to do it with case insensitive method
EDIT:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ${lc:$1}.php [NC]
this way anything entered will be redirected to a php file.
edit : this way your js and css file can still run
RewriteRule ^/[Dd][Ee][Mm][Oo]/?(.*)$ demo.php [NC,L]
will redirect any capitalization of "Demo" to demo.php
First add this line in the <VirtualHost> section OR at the end of your httpd.conf file (to enable lc function in .htaccess for later use):
RewriteMap lc int:tolower
Then have these rules in .htaccess file:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteCond %{THE_REQUEST} ^GET\s.+\.php [NC]
RewriteRule ^(.+)\.php$ /$1 [NE,R=301,L,NC]
RewriteCond %{REQUEST_URI} !\.php$ [NC]
RewriteCond ${lc:%{REQUEST_FILENAME}}.php -f
RewriteRule . ${lc:%{REQUEST_URI}}.php [L]
First rule in .htaccess is doing external redirect by making a URI of /index.php to /index
Second rule in .htaccess is doing internal redirect by makign a URI of /INDEX to /index.php by lowercasing the URI. Assuming you have filename index.php physically present.
That way you can always write URLs without .php extension.