CodeIgniter Modules to Sub Domains - php

I am developing the website in codeigniter with HMVC. I will have multiple subdomains which points to HMVC modules.
My current .htacess contains
DirectoryIndex index.php
RewriteEngine On
RewriteCond $1 !^(index\.php|themes|assets|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
File structure
./application
/modules
/news_module
/mobile_module
/api_module
./system/
It works normal when typing
http://mysite.com/news_module/controller/method
http://mysite.com/mobile_module/controller/method
http://mysite.com/api_module/controller/method
But I want to redirect from
http://mysite.com/mobile_module/controller/method
to
http://m.mysite.com/mobile_module/controller/method
and hide (mobile_module) so finally
http://m.mysite.com/controller/method
In case sombody type
http://m.mysite.com/mobile_module/controller/method
also i want
http://m.mysite.com/controller/method
how can i do this in .htaccess?

for the first part of your question use:
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^mobile_module/(.*) http://m.example.com/$1 [R=301,L]
Don't see how someone would just type m.mysite.com/mobile_module/_______

Related

multiple .htaccess rewrite rules for custom PHP MVC framework

I am trying to build a custom PHP MVC framework, I want to do the following using .htaccess rewrite
localhost/mvc/page1 OR localhost/mvc/page1/ To this
localhost/mvc/index/view/page1
AND
localhost/mvc/blog OR localhost/mvc/blog/To this
localhost/mvc/blog/index
AND
localhost/mvc/blog/blog1 OR localhost/mvc/blog/blog1/To this
localhost/mvc/blog/view/blog1
here index and blog are controllers
Below is what I have tried so far
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?rt=index/view/$1 [L,QSA]
This redirects localhost/mvc/page1 To localhost/mvc/index/view/page1
So basically, I want to have different rewrite conditions for different controllers. So when I will add a new controller I will keep adding condition to htaccess file as well.
This is a code behind redirect (URL should not change),. and everything behind localhost/mvc/ should be reflected in GET $_GET['rt'] parameter
How to achieve this??
Have it this way:
RewriteEngine on
RewriteBase /mvc/
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^(blog)/?$ index.php?rt=$1/index [L,NC,QSA]
RewriteRule ^(blog)/([^/]+)/?$ index.php?rt=$1/view/$2 [L,NC,QSA]
RewriteRule ^(.+)$ index.php?rt=index/view/$1 [L,QSA]

301 redirect URLs that are also being rewritten

I am building a site using Slim, which suggests the following .htaccess code for creating pretty URLs:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
This means that a URL like http://example.com/account/login will alias for http://example.com/index.php/account/login, which is then processed by my front controller. So far so good.
However, if someone explicitly navigates to
http://example.com/index.php/account/login,
I would like it to first redirect to:
http://example.com/account/login,
and then alias for
http://example.com/index.php/account/login.
That way, the user will see http://example.com/account/login in their navigation bar.
How can I handle both of these rules simultaneously in .htaccess, and most importantly, without hardcoding the host and domain (http://example.com) in .htaccess?
This should also work in a subdirectory, for example:
http://example.com/site1/index.php/account/login ->
http://example.com/site1/account/login
With the .htaccess file located in the site1 directory relative to document root, and without having to explicitly put site1 in the .htaccess.
You need a new redirect rule before this rule:
RewriteEngine On
RewriteCond $0#%{REQUEST_URI} ^([^#]*)#(.*)\1$
RewriteRule ^.*$ - [E=BASE:%2]
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^index\.php(?:/(.*))?$ %{ENV:BASE}$1 [L,R=302,NC,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
Try this:
RewriteRule ^index\.php/(.*)$ /$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

HTAccess Rewrite Issues

I am writing a website which is dynamically populated through an Oracle database.
I have completed the desktop site and am now required to create a mobile site. Due to how different the sites are planned to look, I have opted to create 2 different "template" like websites for the mobile and desktop sites.
However, for my desktop site, everything is built off the index.php file in order to allow it to be completely dynamic. Pages are therefore look like www.domain.com/index.php/page in the url.
For the desktop site, this works. I am using a generic index.php removal rewrite rule in order to then make the url www.domain.com/page however still display the same page as the previous URL.
My issue, is that now I have a www.domain.com/mobile/index.php. Which has been created and for the most part has been working, however when trying to add addition dynamic pages to the mobile site. www.domain.com/mobile/index.php/about for example just redirects to www.domain.com/mobile/ and it doesn't even include the about part of the URL.
After much debugging, I have discovered it is definitely the .htaccess that is causing the issue.
If you have any insight into my issue, please help me out.
Thanks in advance
EDIT 1
Rewrite Rules are as follows
# Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
You can use this code in your /mobile/.htaccess file:
DirectoryIndex index.php
RewriteEngine On
RewriteBase /mobile/
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^index\.php(?:/(.*))?$ $1 [L,R=302,NC,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php/$1 [L]
This will override all the rules present in parent .htaccess for /mobile/ URI path.
Simplified version to make it work in root .htaccess:
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^(mobile)/(.*)$ $1/index.php/$2 [L,NC]
# Directs all EE web requests through the site index file
RewriteRule ^(.*)$ /index.php/$1 [L]
Based on the debugging you mentioned, there is a rule in your .htaccess which is rewriting www.domain.com/mobile/index.php/about to www.domain.com/mobile/. So, if you find which rule this is, you can add one above it that will catch requested URLs for your mobile pages and then not allow the problematic following rule to run. Something like this:
RewriteRule ^mobile/index.php/([a-zA-Z0-9-_]+)$ ^mobile/index.php/$1 [L,QSA]
The L ensures that if the user's request matches this rule, no further rules (including the one causing the issue) will be executed.
Thank you for the answers you've both given, however neither of them worked, I've now solved the issue, it was to do with the final RewriteRule at the end
# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
I needed to change it to
# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !/mobile/.* [NC]
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /mobile/.* [NC]
RewriteRule ^(.*)$ mobile/index.php/$1 [L]
So that it would work with both mobile and desktop sites.

CodeIgniter Multi Application htaccess issue

Before you tell me to go to CodeIgniter multi-application .htaccess problem, hear me out.
The reason I say that is because my problem seems to be nearly the exact same as this posters. My directory structure is:
application/
admin/
app/
..
.htaccess
admin.php
index.php
The htaccess rules are:
RewriteEngine On
RewriteBase /dev/
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^admin$ admin\.php [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^admin\/(.*)$ admin\.php/$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index\.php/$1 [L,QSA]
Essentially what I need is:
type in example.com/admin - the url keeps the admin folder, and redirect the user to admin.php
type in example.com/admin/controller/method - same as above
type in example.com/anything_else - the user gets directed to the app instead of admin (index.php)
In testing the person in the aforementioned post's htaccess, which mine are essentially copied from, in an online tester, the two rules involving URIs with 'admin' in it are not passed for some reason. I can only get admin (not ^admin$) to work as a pattern (according to the tester).
RewriteRule ^dev/admin(\/?) admin.php? [L,QSA]
Should do it for you. This will account for /admin, /admin/ and /admin/whatever/here. But for the latter, be aware that since you have QSA on it will append it to the destination URL like so: /admin.php?whatever/here.

remove index.php with alternative routing in codeigniter

I'm working on a project in Codeigniter that doesn't use the standard routing.php file.
It calls the functions like this: test.vi/index.php/controller/function.
My goal is to remove the index.php from the url with a htaccess rewrite:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
And also this config.php change:
$config['index_page'] = '';
My question is do I need to route all the controllers/functions in my route.php or can it be done without using the route.php file?
first make sure you enabled mod_rewrite in httpd.conf:
to do so add/make sure it exsits this line
LoadModule rewrite_module modules/mod_rewrite.so
then create .htaccess in your codeigniter root folder (next to index.php file) not application folder.
.htaccess
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|img|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
codeigniter take care of all routings for you; so for
controlers/welcome.php method=hello u can access it by
http://localhost/welcome/hello
its not like laravel you need to specifically add route; codeigniter automatically Fitch controller for you. unless you have it stored in a different location or inside subfolder then you dont need to touch route.php except for setting your default route.
Here's my redirect in .htaccess for a live codeigniter site. The second part exempts certain directories.
#Redirect non-www to www:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond $1 !^(index\.php|phpinfo\.php|test\.php|example\.php|images|i|css|js|beta|captcha|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
THIS works perfect for me... give it a try..
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-s
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
try this
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /test.v/index.php?/$1 [L]
I guess everyone forget about add test.vi folder name on .htaccess

Categories