.htaccess multiple rewrite rules for codeigniter - .htaccess

I want to ask how can we add an additional rewrite rule(s) in .htaccess file including the index.php removal rule in codeigniter.
Here is my .htaccess file
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^profile/([0-9]+)$ /index.php/front/profileDetail/$1
RewriteRule ^(.*)$ /index.php/$1
The problem is that, only one of the rule works at a time. If I save the file as shown above it returns internal server error!
What I want is completely remove index.php file plus rewrite the url from xyz.com/front/profileDetail/50 into xyz.com/profile/50
Please help what's wrong, I am completely newbie in this regard.
Thanks! in advance.

To elaborate on my comment above, in config/routes, you would put:
$route['profile/(:num)'] = 'front/profileDetail/$1';

Related

Some server giving "No input file specified"

I need help about my htaccess. I am using the following htaccess rules it is working perfectly some servers. But some servers giving No input file specified I have searched some solution on stack overflow.
RewriteRule ^(.*)$ index\.php/$1 [L,QSA]
For example this RewriteRule will fix RewriteRule ^(.*)$ index.php?$1 [L] the No input file specified but some pages are not opening like https://www[dot]webadress[dot]com/settings?tab=page
What do I need to do to fix No input file specified I need QSA in my RewriteRule.
Anyone can help me please.
For non-existing pages uris/requests, you could try following rules. Along with rules please make sure your index.php is residing in same folder where index.php is present. Please clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.php [QSA,L]

adding exception to the htaccess file to bypass a single URL

I'm a newbie with htaccess file so please bear me with my ignorance.
I'm working on a code-igniter project whereby I require to add exception for a particular URL say : www.domain.com/leaveit/file
Basically I have placed a folder at the root which I need to bypass from the rule written in htaccess file where by index.php is added for every request made.
Any suggestion / link would be highly appreciable as I have searched a lot for the solution but didn't end up with anything concrete to help me out of this issue.
Here's my htaccess code.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301]
php_flag output_buffering On
Thank in advance for your time and patience.
Simply try this
RewriteEngine on
RewriteCond $1 !^(index\.php|robots\.txt|leaveit)
RewriteRule ^(.*)$ /index.php/$1 [L]
Let us know if your problem solves or not.

CodeIgniter: add custom rewrite rule to redirect from static file to controller

Lets say that I have a static app and a JSON file that feeds it.
The problem is that the JSON file it's not available yet and I can't use it, so, to make the application run in development environment, I want to add a rewrite rule to redirect the JSON request to a CI controller path that will return a JSON as well.
My htaccess file looks like this
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|assets|static|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
#Rewrite for development
RewriteRule ^static/app/json/appData.json$ /buscador/getAppData
The first condition is the CI main condition to redirect all request to index.php
Then I added the RewriteRule for the JSON file that's going to be located in static/app/json/appData.json and it needs to be redirected to the CI controller buscador/getAppData
The thing is that this Rewrite Rule isn't working and I don't know why, can somebody tell me what is wrong o how can I add a custom rewrite rule in CI?
Thank you!
I was pondering on a similar problem for quite a few hours. I needed to add a custom RewriteRile so that a shorter link should be generated (and working) in order to be sent as a footer in an SMS to users. I finally managed to do it with a 301 redirect before the CI redirects. Here's my .htaccess:
Options -Indexes +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
#Custom redirect
RewriteRule ^nv([0-9]+?)?$ /news/locate/item/$1 [NC,L,R=301]
#Remove trailing slash against SEO duplicates
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)/$ /$1 [L,R=301]
#Generic controller redirects
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Now http://domain.tld/nv1234 redirects to http://domain.tld/news/locate/item/1234 as intended, although not as transparently as I would like it to, but I couldn't find a better way.
Please note the R=301 in the Custom redirect line - the RewriteRule will not work if the client is not redirected to the newsController.
Posting my solution here with the hope that other developers will not get lost as I (and the OP) did.

rewrite rule is not redirecting properly

I would like to be able to use rewrite rules to redirect anyone who opens the link :
domain.com/name
to
index.php?username=name
what would be the best way to do it?
I tried to post the htaccess code I wrote but stackoverflow keeps saying it doesn't meet standards so I removed it.
thanks in advance
Something like this will do it:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/([^/]+)/? [NC]
RewriteRule .* index.php?username=%1 [L,QSA]
It will map silently
http://domain.com/name
To
http://domain.com/index.php?username=name
For this to work, /name must be the first directory in the incoming URL path.

Rewriting URL using .htaccess and mod_rewrite

I used this code in my .htaccess to rewrite my urls which is like this:
www.example.com/subcategory.php?subcat=my-test
to
www.example.com/my-test
.htaccess code I used:
RewriteEngine on
RewriteCond $1 !^(subcategory\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ subcategory.php?subcat=$1 [L,QSA]
My problem is whenever I access the url not pertaining to my subcategory.php file like contactus and aboutus, the .htaccess file will somehow put me to subcategory.php file which is not what I want. I want my contactus be handled by contactus.php and aboutus with aboutus.php.
I know that there is something wrong with my .htaccess file but I couldn't fix it by myself for I am not so familiar with the .htaccess coding.
Any suggestion would be greatly appreciated.
Thank you very much!
You are rewriting all requests to subcategory.php. There is no way for .htaccess to tell whether /xxx is a subcategory or some different page, so you could redirect it to a different script.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L]
Use this code snippet instead of yours and move all the "routing" logic to PHP. In PHP, you can find out whether /xxx is a subcategory, a contact page, an article or something else and use a script suitable for that kind of database record.
You will find the requested URL in $_SERVER['REQUEST_URI'].

Resources