htaccess and MVC routing - model-view-controller

Well i started coding a mini MVC base that I can just use on client websites, which also uses the same type of router class as CakePHP. I currently have the url display like this:
http://localhost/mvc/index.php?page=blog/viewall
Which gets the controller and tells it the function, just like CakePHP index(), view(), and so on.
Now, I'm not sure how I can make the url look like this with .htaccess
http://localhost/mvc/blog/viewall
I was trying to add this in the .htaccess file:
RewriteRule ^(.*)$ index.php?page=$1 [QSA]

Have your redirect rule like this:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/mvc/index.php/.*
RewriteRule ^/mvc/(.*) /mvc/index.php?page=$1 [QSA,L]

Related

Laravel - Using htaccess to manually pass paramaters to index.php

I'm having Laravel build images for me, but once the image exists, I'd like Apache to serve it (instead of booting Laravel, reading in the image, and passing it through). Here is the rule I wrote in htaccess to do this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^/storage/product_images/(.*).png
RewriteRule ^storage/product_images/(.*)\.png$ index.php/image/$1 [L]
My question is about this last bit: index.php/image/$1 in which I am trying to pass values into Laravel as though it were a route. This doesn't seem to be working. I'm just getting a 404 from Laravel even though the actual route works (/image/23). Is there a different way to do this? Maybe something like this?
index.php?_url=image/23
... another way to phrase this question: how might I use Laravel without an .htaccess? How could I simply pass params to the index.php file?

CodeIgniter: keep index.php in url and hide the rest

greatings...
Question :
How to keep index.php in url and hide everything else so my url look like (www.base_url.com/index.php) for every page?
Details :
i've searched but i only got how to remove index.php, i think im the only one who want to keep it...lol
im using codeigniter 3 to working on web based app human resource managemen soo i dont need SEO url since the app only accessed by internal employees. i like to make the url clean, only show base_url and the index.php for every page. please show me how to config codeigniter like i need...thank you
Use .htaccess like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
This will look more cleaner than with index.php. The URL will be like this www.your_baseurl.com/users
and edit your config.php file to use URI like this:
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';
You can use your route like this:
$route['users'] = 'home/users';

dynamic subdomains with htaccess: URL shouldnt change in the browser

Trying to implement subdomains with htaccess.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www.
RewriteCond %{HTTP_HOST} ^([a-z0-9]+)\.domain.com(.*)$
RewriteRule ^(.*)$ http://domain.com/index.php?/public_site/main/%1/$1 [L]
</IfModule>
when i enter ahser.domain.com the browser URL is changing. is there a htaccess option to not let this happen when absolute URLs is used in RewriteRule?
Don't rewrite to a full URL with domain in it. That generates a redirect since it's going to a different website! You could put microsoft.com there; so how would it work without redirecting?
What you have to do is make sure that the web pages work under the original domain. So when the client asks for myname.domain.com/... how about rewriting that to myname.domain.com/index.php?public_site/main/myname/.... Keep the domain the same. The index.php? can be made to work in any of those domains. For instance, even this could work:
http://OTHER.domain.com/index.php?public_site/main/MYNAME/...
I.e. set it up so it doesn't matter which virtual host accesses that path.
Once you have that, the rewrite can then just do:
# will not trigger redirect
RewriteRule ^(.*)$ /index.php?/public_site/main/%1/$1 [L]
You have to be careful not to introduce a loop since you're now redirecting a URL to a longer URL which matches the same rewrite rulethe same domain. You need an additional RewriteCond not to apply this rewrite if the URL already starts with /index.php?public_site/.

Insert Yii module string in URL using mod_rewrite for mobile browser

I want to rewrite URL to access Yii module when a mobile browser detected. In Yii, I make a new module called mobile. I don't use theming (which is the common method for mobile site) since I intent to implement quite different logics for normal browser and mobile browser user.
The mobile module will be accessed using http://localhost/project/mobile (or http://project/mobile in production).
The default .htaccess file for Yii is below
RewriteEngine on
RewriteBase /project/
#RewriteBase / #for production
# 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
The problem is I want to insert string mobile for every URL accessed by mobile browser.
for example
http://localhost/project/user -> http://localhost/project/mobile/user
http://localhost/project/login -> http://localhost/project/mobile/login
http://localhost/project/books/1 -> http://localhost/project/mobile/books/1
or for production.
http://project/user -> http://project/mobile/user
http://project/login -> http://project/mobile/login
http://project/books/1 -> http://project/mobile/books/1
Anyone knows what the new .htaccess for this will be?
I don't mind changing few variables for development and production server and for the mobile browser detection I guess I can use the rule from http://detectmobilebrowsers.com
Try this one
RewriteEngine On
RewriteBase /project/
# Redirect /project/*/ to /project/mobile/*/
RewriteCond %{REQUEST_URI} !^/project/mobile/.*$
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera|mobile|palmos|webos|googlebot-mobile" [NC]
RewriteRule ^(.*)$ /project/mobile/$1 [L,R]
# Forward request to Yii
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
It was adapted from .htaccess showed in here http://ohryan.ca/blog/2011/01/21/modern-mobile-redirect-using-htaccess/
The easiest thing you can do here is group all mobile functionality under a "mobile" module, and use Yii's PreRouter functionality to redirect the user in Yii itself. This gives you a lot more control about everything.
class PreRouter
{
public function routeRequest($oEvent)
{
$oApp = $oEvent->sender;
if (<isMobile>)
$oApp->defaultModule = 'mobile';
}
}
In your config you add:
'onbeginRequest' => array('PreRouter', 'routeRequest'),
Under the main array (so NOT under modules, it's an app setting).
As said, this gives you a lot of control over everything. You can determine when the user is to be redirected (for example iPad is also a mobile but a lot of people want the regular site). With this method you can store for example in a cookie that they want to remain on normal site instead of mobile etc.
I know its not with rewrites as you requested but I still hope you find the answer usefull :)

htaccess rewrite

I would like to rewrite /anything.anyextension into /?post=anything.
eg:
/this-is-a-post.php into /?post=this-is-a-post or
/this-is-a-post.html into /?post=this-is-a-post or even
/this-is-a-post/ into /?post=this-is-a-post
I tried
RewriteRule ^([a-zA-Z0-9_-]+)(|/|\.[a-z]{3,4})$ ?$1 [L]
but it doesn't work.
Any help appreciated.
If you have access to the main server configuration, use this:
RewriteRule ^/(.+)\.\w+$ /?post=\1 [L]
If not, and you are forced to put this in a .htaccess file, you could try
RewriteRule ^(.+)\.\w+$ /?post=\1 [L]
In either case, this assumes you will only be rewriting URLs with a single path component (i.e. if you get a request like /path/anything.anyextension it might not work as you expect, the rewrite rule would need to be modified to handle that)
You need a better way to determine when to apply the rewrite rule, otherwise your page won't be able to display external JS or CSS, unless you define an exception.
SilverStripe (or the core, Sapphire) offers a good approach to this, something like:
RewriteEngine On
RewriteCond %{REQUEST_URI} !(\.css)|(\.js)|(\.swf)$ [NC]
RewriteCond %{REQUEST_URI} .+
RewriteRule ^([^\.]+) /?post=$1 [L,R=301]
This requires the URI not to be empty, not to be JS, CSS or SWF, and redirects back to your root directory:
http://localhost/this-is-a-post.php
http://localhost/?post=this-is-a-post
If you don't want a redirection, but the processing, remove the redirection rule R=301

Resources