Mod_rewrite Rules for MVC Architecture - model-view-controller

In my site you can open pages like this...
example.com/?controller=index&action=index
I want to make links nicer so I use mod_rewrite...
RewriteRule ^([^/]+)/(.*)$ index.php?controller=$1&action=$2 [L,QSA]
It works perfectly and result is somehow like this...
example.com/index/index
But my problem is that this link, where is set only the 1st variable won't work. Don't know how to fix it! This won't work...
example.com/index

Do something like this:
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/(.*)$ index.php?mod=$1&act=$2 [QSA,L]
RewriteRule ^([a-zA-Z0-9_-]+)/(.*)$ index.php?mod=$1 [QSA,L]
RewriteRule ^(.*)$ index.php [QSA,L]

Related

CodeIgniter and specific rewrite rule

On my CodeIgniter site, I would like to add a specific rewrite rule, so that this url
http://www.exemple.com/cache.manifest
would rewrite to
http://www.exemple.com/controller/manifest
(because Safari 7 seems to only accept .manifest files for ApplicationCache)
So I try to add this line to my htaccess
RewriteRule ^cache.manifest$ controller/manifest
I added it before the other rewrite rules :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^cache.manifest$ controller/manifest
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
But it returns a 404. If I change the line to
RewriteRule ^cache.manifest$ test.html
it works. So the first part on my rule is correct.
If I try to access directly to www.example.com/controller/manifest, it works to, so my url is correct.
I tried also
RewriteRule ^cache.manifest$ index.php/controller/manifest [L]
But it doesn't work either…
Any clue ?
Thanks a lot
I tried some tests on my local server and I think the following might work:
RewriteRule ^cache.manifest$ /index.php/controller/manifest [R,L]
I am not entirely sure if you need the leading "/" or "/index.php", so you
may need to experiment.
You need the [R] flag to force a redirect. In this situation, you want Apache
to look for the string cache.manifest in the URL, and then go to the CI page
controller/manifest.
It appears that you need to explicitly set the redirect.
Please let me know if this works. Good luck!

Domain Url Rewrite

What I'm looking to do is rewrite the url so that one of the directories are hidden. For example;
http://www.example.com/home/example.html
to
http://www.example.com/example.html
As I'm new to .htaccess and mod_rewrite, is this something that can be done?
This should work:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/home/.*$
RewriteRule ^(.*)$ /home/$1 [L]
Something like this should work.
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^example.html$ /home/example.html [NC,L]

Creating SEO friendly URLs with htaccess

when I change the link It shows me an error page. maybe there is a problem with my website what do you think?
There is the url in my website: www.mywebsite.com/picture.php?1
and I want to change it to something like this: www.mywebsite.com/picture/1
here is my .htaccess
RewriteEngine on
rewritecond %{http_host} ^website.com [nc]
rewriterule ^(.*)$ http://www.mywebsite.com/$1 [r=301,nc]
RewriteRule ^contact\/?$ contact.php [L]
RewriteRule ^terms\/?$ terms.php [L]
**RewriteRule ^picture/(.*)/$ picture.php?$1 [L]**
Please help,
I have no clue what is the problem.
Try adding
<base href="www.mywebsite.com" />
At the top of your page, this might work.
EDIT:
Try using this rewrite rule
RewriteRule picture/(.*)/$ /picture.php?$1 [L, NC]
assuming that the first part of your htaccess works, replace your last line with the following:
RewriteRule ^picture/(.*)$ /picture.php?$1 [NC]

mod_rewrite- matching everything after domain

This rule works fine for http://foo.com/page/contact
RewriteRule ^page/([^/\.]+)/?$ index.php?page=$1 [L]
But what I want to do is for http://foo.com/contact
But this is not correct:
RewriteRule ^/([^/\.]+)/?$ index.php?page=$1 [L]
How do I correct that?
Remove your first / that's not needed because the root directory is already there. like so:
RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [L]
your second example is technically asking for http://foo.com//contact

Mod_rewrite Help

This is what I have, but it is not working.
RewriteRule ^location/([a-zA-Z0-9_-]+)/reviews$ location.php?purl=$1&page=reviews [L]
RewriteRule ^location/([a-zA-Z0-9_-]+)/reviews/$ location.php?purl=$1&page=reviews [L]
Can anyone show me what I am doing wrong?
http://www.example.com/this-location-1234/reviews/ to http://www.example.com/location.php?purl=this-location-1234&page=review
You should combine those two (nearly identical) rules, and make sure you have the RewriteEngine on and your rule is being run (either your .htaccess is getting picked up, or you have this in an active vhost definition).
RewriteEngine on
RewriteRule ^location/([a-zA-Z0-9_-]+)/reviews/?$ location.php?purl=$1&page=reviews [L]
I fixed it. I accidentally was missing some verbiage in the link I was getting an error on.
RewriteRule ^location/([a-zA-Z0-9_-]+)/reviews/?$ location.php?purl=$1&page=reviews [L]
This works great!
I think this should work for the example URLs you given:
RewriteRule ^([a-zA-Z0-9_-]+)/reviews/?$ location.php?purl=$1&page=reviews [L]
(replace the two rewrite rules with this one)
If you don't already have one, add RewriteEngine on before this RewriteRule.
If you actually wanted to rewrite /location/this-location-1234/reviews/ instead of /this-location-1234/reviews/, then use this:
RewriteRule ^location/([a-zA-Z0-9_-]+)/reviews/?$ location.php?purl=$1&page=reviews [L]
I assume you have another property in your conf file called RewriteEngine on ?
Also, you can turn on the logging with RewriteLogLevel.

Resources