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
Related
I'm trying to do a rewrite, where if the page doesnt exist, it rewrites to a category page. Its a bit different to the normal RewriteCond %{REQUEST_FILENAME} -f as the page name is dynamic based on the URL.
If somebody visits a dynamic path name such as "/contacts"
I first want it to check if the following folder/file exists based on the path name:
/[dynamicpath]/[dynamicpath].php
example:
/contacts/contacts.php
If that doesnt exist, rewrite to the following page
/categories/category/category.php?cat_url=[dynamicpath]
example:
/categories/category/category.php?cat_url=contacts
I've tried the below, but it fails with the first RewriteRule triggering when the file exists and goes straight to the category rewrite.
RewriteCond %{SCRIPT_URL} ^(.+)
RewriteCond %{DOCUMENT_ROOT}/%1/%1.php !-f
RewriteRule (.+)/?$ $1/$1.php [QSA,L]
RewriteRule ^([\w-]+)/?$ /categories/category/category.php?cat_url=$1 [QSA,L]
You may use these rules in your site root .htaccess:
RewriteEngine On
# attempt rewrite to /[dynamicpath]/[dynamicpath].php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1/$1.php -f
RewriteRule ^(.+?)/?$ $1/$1.php [L]
# else rewrite to categories/category/category.php?cat_url=...
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/?$ categories/category/category.php?cat_url=$1 [QSA,L]
I found the answer by checking the server variables being passed and discovered %{REQUEST_FILENAME} included the full file path (document_root+filename).
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}%{REQUEST_URI}.php -f
RewriteRule ^(.+)/?$ $1/$1.php [L]
RewriteRule ^(.+)/?$ categories/category/category.php?cat_url=%{REQUEST_URI} [QSA,L]
It is quite popular question, but there is some distinction in my case that makes all more difficult.
Apache2 web server, Yii2 framework with prettyUrl turned on.
Here is initial .htaccess file, provided by Yii2:
RewriteEngine on
# 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
There is /docs web directory. And I would like to get files from it if they are exist and redirect to /docs-generator/create?file=<file_name> if the file doesn't exists.
So, I have added a rule:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^/docs
RewriteRule ^docs/([\w-_.]+) http://kz_proj.test/docs-generator/create?file=$1 [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
It's works but quite strange... First of all, I can't remove my test hostname from http://kz_proj.test/docs-generator/create?file=$1. It doesn't work without full name. The second one is about ^/docs and ^docs/([\w-_.]+) without the leading character. Why first part of these rules can't be the same in my case?
I guess something wrong with the rules...
You can use:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^docs/([\w-_.]+) /docs-generator/create?file=$1 [L,R=301]
# 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
It works with the domain name because it is necessarily a redirect in this case, but not without. I added [R=301] to force the redirection.
No need to add RewriteCond %{REQUEST_URI} ^/docs
because the test is the same with RewriteRule ^docs
And why first part of these rules can't be the same in your case ?
REQUEST_URI always start with a /
and RewriteRule test path never start with a / in .htaccess (why????)
hi guys i am trying to rewrite a url but getting errors
i would like to rewrite
www.example.com/photos.php?u=username
to
www.example.com/#username/photos
here is my code
IndexIgnore *
RewriteEngine On
Options +FollowSymLinks
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^#([A-Za-z0-9]+)$ user.php?u=$1 [NC,L]
RewriteRule ^#([A-Za-z0-9]+)$/^([A-Za-z0-9]+)$ user.php?u=$1 photos.php?u=$1 [NC,L]
Your first RewriteRule looks fine, but your second RewriteRule is full of errors.
You're trying to feed the right hand of the rule with 2 different address as seen at:
user.php?u=$1 photos.php?u=$1
Your rule on the left hand have a terminator sign at the middle of the regex and after it you start a new regex which would not work as well:
^#([A-Za-z0-9]+)$/^([A-Za-z0-9]+)$
This should work for your needs and it should be placed inside your .htaccess file, which should be located inside your domain ROOT folder along with both the photos.php and the user.php files or it will not work.
IndexIgnore *
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^#([a-zA-Z0-9]+)/photos$ photos.php?u=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^#([a-zA-Z0-9]+)$ user.php?u=$1 [QSA,L]
If you have a different folder setup then update your question with more information on it so I can update the rule to fit it.
The above rules will allow you to access the photo page in the following format:
www.example.com/#username/photos
And the users page in the following format:
www.example.com/#username
When I don't edit my .htaccess file, my image path reads something like: http://this.website.com/codeigniter/inc/images/logo.jpg.
However, as soon as I add this code into the .htaccess file:
RewriteEngine On
RewriteCond $1 !^(index\.php)
RewriteRule ^(.+)$ index.php?$1 [L]
(to remove the index.php from the URL), I get 404 error, even with the exact same path.
How do I remove index.php and still have access to my images?
RewriteEngine On
RewriteCond $1 !^index\.php [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?$1 [L]
The two new RewriteConditions would exclude existing directories and files (and so images) from redirection.
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