How to configure apache to allow rewrite rules for directories? - php

I have some configuration in htaccess:
RewriteEngine On
RewriteRule ^(.*)$ /index.php [L,NC,QSA]
All urls redirects to /index.php
But if folder exists, it goes to folder, not to /index.php
why?
I tried this solution to make this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule . index.php [QSA,L]
But this like a previously example goes to directory =(

Try this
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php
Or
RewriteEngine On
RewriteCond %{REQUEST_URI} !-f
RewriteRule (.*) index.php [QSA]

Use something like this:
#turn rewrite engine on
RewriteEngine On
#specify your base
RewriteBase /
#make sure, your index.php won't be rewritten
RewriteRule ^index.php - [L]
#rewrite everything else
RewriteRule ^(.*)$ index.php?para=$1 [QSA,L]

But if folder exists, it goes to folder, not to /index.php
Try disabling MultiViews – it is often interfering in such cases.
Options -MultiViews
http://httpd.apache.org/docs/2.2/mod/core.html#options
http://httpd.apache.org/docs/2.2/mod/mod_negotiation.html#multiviews

Related

Change root path from .htaccess

I would like to change "/" to "/folder".
So when user clicks link with this url "/page.php" I want it to be automatically translated by .htaccess to "/folder/page.php".
How can I achieve this?
This simple rule should work for you in site root .htaccess (parent directory of folder):
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/folder/ [NC]
RewriteRule .* folder/$0 [L]
Please use .htaccess file. i think its better for you.
Your Need is:
HTTP://YOUR_DOMAIN.COM/page.php
TO
HTTP://YOUR_DOMAIN.COM/FOLDER/page.php
Step1:
create an .htaccess file on your Server
Filename: .htaccess
just Copy this part and Change the Values.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} YOUR_DOMAIN.COM$ [NC]
RewriteCond %{HTTP_HOST} !FOLDER
RewriteRule ^(.*)$ http://YOUR_DOMAIN.COM/FOLDER/$1 [R=301,L]
Reference-1 |
Reference-2
Try this:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /folder/$1 [QSA,PT,L]
RewriteRule ^$ /folder/$1 [QSA,PT,L]
RewriteRule ^index.php/(.*) $1 [QSA,R,L]
</IfModule>

Silex routing .htaccess webroot

I'm using the Silex "micro-framework" for routing purposes of my application.
I'm currently stuck on how to rewrite the url with .htaccess.
Standard Silex url: localhost/myapp/web/index.php/hello/name
I want it to look like: localhost/myapp/hello/name
With the following .htaccess code, I'm able to omit the /index.php/ part. But I still have to use the /web/ part.
RewriteEngine On
RewriteCond %{THE_REQUEST} /myapp/web/index.php/
RewriteRule ^/myapp/web/index.php/(.*) /myapp/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !/myapp/web/index.php/
RewriteRule ^(.*)$ /myapp/web/index.php/$1 [L]
Any suggestions?
Thanks!
I've faced the same problem right now and the solution was to change .htaccess content to:
FallbackResource /index.php
So in your case it would be
FallbackResource /web/index.php
This worked for me and I hope someone will find it useful.
From: http://silex.sensiolabs.org/doc/web_servers.html
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /web
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
</IfModule>
The right would be to set up your DocumentRoot to /path/to/your/app/web.
Something like this should do the trick:
RewriteEngine On
RewriteBase /
RewriteRule ^myapp/web/index.php/ /myapp/ [R=301,L]
RewriteRule ^myapp/ /myapp/web/index.php/ [L]
I had a similar problem, solved with the following
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /web
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)? index.php/$1 [QSA,L]
RewriteRule ^/?$ /web/ [R=301]
</IfModule>

RewriteRule on alias url

on query:
domain.com/page/do
i need:
domain.com/index.php/page/do
etc.
i use htaccess file, i add alias
RewriteBase /index.php/
How i write RewriteRule to this url?
Try this, it should do what you want, and eliminate the need to have index.php in your uri
###
# Rewrite Rules
#
<IfModule mod_rewrite.c>
### Enable mod_rewrite
RewriteEngine On
RewriteBase /
### Checks if the URI request path coincides with a literal path
### on the filesystem relative to the webroot. If it does not exist,
### then it passes the path through index.php.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
Try this:
RewriteRule ^(.*)/(.*)$ /$1/$2 [L]
Add these rules to the htaccess file in your document root:
RewriteEngine On
RewriteBase /
RewriteCond ^/index.php
RewriteRule ^/?(.*)$ /index.php/$1 [L]

How do I remove the '.php' extension from URLs?

This is where the PHP files are:
ts1.WEBSITENAMEHERE.biz/cp/
The .htaccess file is also in this folder.
I want to remove the '.php' from 'FILENAME.PHP'.
This is my current '.htaccess' file:
Options +FollowSymlinks
RewriteEngine on
RewriteBase /cp
RewriteRule ^(.*)/$ $1.php
But it is not working. Why is this?
RewriteEngine on
RewriteBase /cp/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !\.php$
RewriteRule .* $0.php [L]
Create a .htaccess file in the DocumentRoot dir.
Add the following code in that file...
##############
RewriteEngine On
RewriteCond %{REQUEST_URI} !^(.*)\.(.*)$
RewriteRule ^(.*)$ %{REQUEST_URI}.php [R]
#############
mod_rewrite should be enabled for this to work.
Options +FollowSymlinks
RewriteEngine on
RewriteBase /cp
RewriteRule ^(.*) $1.php
I use this checker: http://htaccess.madewithlove.be

.htaccess in subfolder

The main site runs WordPress with .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
I'm going to create an CodeIgniter app in subdirectory site.com/online/
If simply add CI .hta file in subfolder it wouldn't work. CI .htaccess:
RewriteEngine on
RewriteCond $1 !^(index\.php|robots\.txt|img|styles|js)
RewriteRule ^(.*)$ /index.php?/$1 [L]
Is it possible to combine two .htaccess files or to do something with .htaccess in CI subfolder?
Thank you.
UPD.
Thank you for answers, I've tried all variants, and eventually moved project to subdomain.
First thing, your .htaccess should be like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} !^GET\s/+online/ [NC]
RewriteRule . /index.php [L]
</IfModule>
#Then add this for CI handling in the same .htaccess
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1 !index\.php [NC]
RewriteRule ^online/(.*)$ online/index.php?/$1 [L,NC]
Then you can remove (or rename) .htaccess in the online subfolder.
Try this for the codeigniter .htaccess:
RewriteEngine on
RewriteBase /online
RewriteCond $1 !^(index\.php|robots\.txt|img|styles|js)
RewriteRule ^(.*)$ /index.php?/$1 [L]
Otherwise all requests to that subfolder would be redirected down to wordpress.
Are you setting AllowOverride? According to the docs you need to allow FileInfo overrides for mod_rewrite.

Categories