Help in configuring .htaccess to exclude a path from rewriting - .htaccess

I have a root folder with the following .htaccess configuration
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
What I need is, I have installed a wordpress site in a sub directory 'blog/'. Now I am able to access the home page of the blog since I have givien RewriteCond %{REQUEST_FILENAME} !-d. But clicking on a post will redirect me to the index file in root folder. How can I solve this. Please help. Thanks in advance.

Add a RewriteCond to exclude paths which begin with "blog/"
Something like (added some existant rule for context)
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/?blog/
Not tested, but you get the idea

Related

.htaccess file - Redirecting/Masking to hidden directory

I have the below code that we add into the root of a domain name that masks the directory of wordpress to try and keep it hidden. I would rather not have to specify the domain name in the .htaccess file so I can just upload the same file to all the sites. Can someone help suggest how I can alter this to make it work with any domain?
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?domainname.com$
RewriteCond %{REQUEST_URI} !^/wordpress-hidden/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /wordpress-hidden/$1
RewriteCond %{HTTP_HOST} ^(www.)?domainname.com$
RewriteRule ^(/)?$ wordpress-hidden/index.php [L]

Separate .htaccess for folder, ignoring previous one

I have this .htaccess in my root folder:
root:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1&%{QUERY_STRING} [NC,L]
So that incoming GET requests come through http://www.example.com/somethingsomething instead of ?page=somethingsomething. That's all fine.
However, in the root folder I have another folder named /i. Id like this one to handle another type of request, which looks like this: http://www.example.com/i/somethingsomething with the ending actually meaning http://www.example.com/i/index.php?img=somethingsomething. Problem is that the .htaccess in the root folder is still in use. What I need is the following:
/i:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?img=$1&%{QUERY_STRING} [NC,L]
, but somehow exclude the .htaccess in the root folder.
Is this possible?
EDIT:
Tried what I found on this site, for instance using RewriteCond %{REQUEST_URI} !^/(i) and similar in the root .htaccess.
Yes, you can do it like this, using a single rule in the root .htaccess. The root .htaccess is always going to get processed, so you might as well do it there with one rule. Otherwise it would be more complicated and the root would need modifying with an exception for /i anyway.
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/(?:i/)?index\.php$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(i/)?(.*)$ /$1index.php?img=$2 [QSA,NC,L]
Using the [QSA] flag which is a better way to pass on any existing query string. RewriteBase is not needed. You perhaps don't need the directory check and it would be better for performance without it. The index.php check is there to improve performance by avoiding another file-system check after a successful rewrite.
Update
Sorry, I hadn't noticed that the parameter you are passing to index.php has a different name in the second case. These rules in root should work for you:
RewriteEngine on
RewriteCond %{REQUEST_URI} !=/index.php
RewriteCond %{REQUEST_URI} !^/i/ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?page=$1 [QSA,NC,L]
RewriteCond %{REQUEST_URI} !^/(?:i/)?index\.php$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^i/(.*)$ /i/index.php?img=$1 [QSA,NC,L]

Codeigniter htaccess for multiple applications

Hello guys can you help me how set proper url in codeigniter using multiple applications and i proper .htaccess
Folder Structure
-application
-api
-cms
-system
-api.php
-cms.php
Its works when a use this
http://localhost/kitkat_funs/cms.php/
http://localhost/kitkat_funs/api.php/
But i want this
http://localhost/kitkat_funs/cms/
http://localhost/kitkat_funs/api/
My htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
Please try following steps:
In config=>config.php, replace like following.
$config['index_page'] = 'index.php';
to
$config['index_page'] = '';
Please put following code into your .htaccess file, this might help you to achieve the result.
write your project folder name as rewritebase
RewriteBase /yourprojname/
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/system.*
RewriteRule ^(.*) index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+) index.php?/$1 [L]
*take backup of your .htaccess file.
This problem appear when you working with Codeigniter using Multiple application(and multiple index_appname.php. Ofcourse you can add new line in .htaccess everytime you add another application but that is not convenience.
The solution is using basic Regular Expression. You need to write 2 section, one is for address without slash, and the other with more than one slash after index file
Note: in this case the index.php file has prefix "index_" following with app name
# If the user types without trailing slash example www.host.com/kitkat_funs/appname
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(\w*)$ index_$1\.php [L,QSA]
# If the user enter in any system section with slash example www.host.com/kitkat_funs/appname/page/etc
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\/(.*)$ index_$2\.php/$1 [L,QSA]
Everytime user enter address right after domain name, for example www.host.com/kitkatname/cms
.htaccess will automatically redirect to www.host.com/kitkatname/cms.php
now you can get rid .php extension on your index file

Use .htaccess file to check folder for file and redirect if not found

I want to use .htaccess files so that if someone visits an old URL on our new website such as website.com/oldpage.asp .htaccess first checks to see if website.com/oldsite/oldpage.asp is on our server. And if no file is there redirect them to old.website.com/oldpage.asp where a copy of our old website exists.
For some reason only the .htaccess file in the /oldsite/ folder is executing.
Here is my .htaccess file for my site root which checks if the file exists on the new site and if not redirects to /oldsite/
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /oldsite/$1 [NC,QSA,L]
Then in /oldsite/ its supposed to check if the file again exists, and if not redirect again.
RewriteEngine On
RewriteOptions inherit
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://old.website.com/$1 [R=301,NC,QSA,L]
You don't need 2 .htaccess for this.
Have this rule in site root .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/oldsite/$1 -f
RewriteRule ^(.*)$ oldsite/$1 [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://old.website.com/$1 [R=301,L]
Then remove /oldsite/.htaccess and test this after clearing your browser cache.

codeigniter htaccess with multiple applications

I am using codeigniter. I have front end application and backend application like
/system/
/application/
/front/
/admin/
index.php
admin.php
.htaccess
I want my url like http://example.com/news/article1 (for site)
http://example.com/news/admin (for admin)
In .htaccess I have written
RewriteEngine On
# If the user types just "admin".
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/admin$ admin\.php [L,QSA]
# If the user enter in any admin section, like "admin/section".
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/admin\/(.*)$ admin\.php/$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
the front end is working fine but when I enter mydomain.com/admin it is throwing 404 not found error. Please help me.
Thanks and regards
I'm not that good with .htaccess, but from the CI website, you can use this for your .htaccess rule
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
and then use routes to rewrite your urls. Much easier than trying to do it in .htaccess files

Resources