Handling routing with .htaccess - .htaccess

I've just learned a bit of CodeIgniter Framework and I saw something which I wondered a long time ago.
CodeIgniter handles Routing just as this:
http://mydomain.com/index.php/classname/function/parameter
Now I want to get this to work:
http://mydomain.com/classname/function/parameter
Is it somehow possible to get the webserver to not check for the directories but to forward those parameters to mydomain.com/index.php? Is this somehow realizable by .htaccess or something?
Thank you for help!

Here is the help regarding this http://codeigniter.com/user_guide/general/urls.html
or Place
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
in your .htaccess

Related

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.

specific redirect required using mod_rewrite

I have to do a very specific url redirect using mod_rewrite within an .htaccess. Below is a url which has to map to the url below it:
m.example.com/123456/123456-product-name/
This needs to map to the following:
m.example.com/product-name/123456
I'm still getting to grips with regex and url rewrites and I've spent a good couple of hours trying to get this right. Can anybody help!?
Thanks in advance
You can do that. in root .htaccess:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^\d+/(\d+)-([^/]+) /$2/$1 [R=301,L]
Or just [L] (and not [R=301,L]), if you do that without redirection.

url rewriting not working on my server

I am having some problems with my url rewriting in my .htaccess. They work well locally but for some reason i don't understand, they don't work on my server. I was able to make my first rule work by adding a question mark after the index.php like so (i also don't understand why it needs this question mark) :
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index\.php?/$1 [L]
But i still cannot find a way to make this rule work on my server.
RewriteRule article/([a-zA-z0-9\-]+)-([0-9]+)/ article/$1/$2/ [L]
Could you tell me what am i doing wrong and why it works locally but not on my server ? Thanks !!
I'm guessing you need the question mark because your server isn't setup to handle PATH_INFO. But you need the article rule before your other rule:
RewriteRule article/([a-zA-z0-9\-]+)-([0-9]+)/ article/$1/$2/ [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index\.php?/$1 [L]
I finally managed work this out. I looked at the $_SERVER variable on my server and found out that the rewritten url was stored in the $_SERVER['QUERY_STRING']. Don't ask me why ...
So i am using this variable instead of $_SERVER['PATH_INFO'] and every things works perfectly again.
URLS like this : /article/lala-00000000164/ are invisibly rewritten as so : /article/lala/00000000164/.
Thanks for the help !!

htaccess, rewrite multiple pages to the same page but keep original value somewhere

I have been fiddle farting around with htaccess and RewriteEngine but I can't quite get my head around it...
I'm building a website on which I want users to be able to go to /portfolio/typography for example. But I don't want to create seperate pages for each category in this portfolio and thus I want to rewrite (redirect?) all the requests that go to /portfolio/ to the index.php of this directory and load the appropiate projects for this category from there.
Any ideas on how I could do this? I used this to redirect all the requests to /portfolio/:
RewriteCond %{REQUEST_URI} !/portfolio/$ [NC]
RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif) [NC]
RewriteRule .* /portfolio/ [R=302,L]
Thanks in advance,
Cas Cornelissen
EDIT
Maybe I should note that I have another .htaccess file in the root of my website.
Ok, so I found the answer to my own question...
Seems like the following is working:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1?%{QUERY_STRING} [L]

IIS url rewrite Error 404.0 [edited]

I want:
domain.com/example
to target:
domain.com/folder/example.cshtml
Can anyone help me with the rewrite rule for it?
Every suggestion is much appreciated :)
I have edited the question so that it actually focuses on what is important.
why aren't you using mod-rewrite?
RewriteCond %{THE_REQUEST} /folder/example.cshtml
RewriteRule ^$ /example.cshtml [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^example.cshtml /folder/example.cshtml [L]
Sometimes you need to mess around with the slashes a bit
I guess it can't be done the way i want it.
I'll have to move example.cshtml to the root directory.

Resources