I am creating an application with Yii Framework and want to redirect http://mydomain.com/yiiframework to http://mydomain.com/yiiframework/default so i created .htaccess file and i put this content on it but it don't redirect anything :
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /default/index.php [L]
my questions are :
1 - what is wrong in my htaccess file ?
2 - is that a good way to create admin section and put other stuff in default folder ?
Edited :
I accidently find it out ,i changed .htaccess to this :
RewriteEngine on
RewriteRule ^$ http://mydomain.com/yiiframework/default/index.php
and it server my purpose , but the mystery is :
^$
shows the start of line and the end of the line , what this Regular Expressions means?
to create admin-panel best practice is to create separate application for backend and frontend, which share common things with fontend, backend as in this boilerplate,https://github.com/clevertech/YiiBoilerplate
or
you sholud create single application and devide it into modules, each module have code for both frontend and backend as you see this in many yii based open source applications like Zurmo, Yupe CMS, X2Engine,
i would prefer second approach which is modular and maintainable code, slightly hard but best
Related
I have a scenario where I want something similar to Codeigniter.
In Codeigniter my url is like:
http://www.example.com/filename/methodname
Now I want similar thing but using plain core PHP and .htaccess.
How is that possible ?
I want to have a index.php inside my folder and then redirect the http requests accordingly.
Searching the web I found this :
RewriteEngine on
RewriteRule ^([a-z0-9_-]+)\.html$ index.php/page/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|asset|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
But I have little knowledge of .htaccess and don't know how this could help.
So I want an answer with example to understand how this can be achieved using .htaccess.
What would I need to do on my PHP side ?
Routing
Having urls like /filename/methodname is generally called routing. You have half of it done already; what you show in .htaccess is the part that will redirect all traffic towards an index.php file.
# starts rewrite engine
RewriteEngine on
# redirects direct .html page calls to their corresponding pages
RewriteRule ^([a-z0-9_-]+)\.html$ index.php/page/$1 [L]
# for anything that is not a file
RewriteCond %{REQUEST_FILENAME} !-f
# and for anything that is not a directory
RewriteCond %{REQUEST_FILENAME} !-d
# except if it's a robot
RewriteCond $1 !^(index\.php|asset|robots\.txt)
# send all that to index.php
RewriteRule ^(.*)$ index.php/$1 [L]
That index file will then parse the url and call relevant handlers with relevant arguments based on what matched.
How to create one such parser, or router, is beyond the scope of a single answer, but basically depends on the use of $_SERVER['REQUEST_URI'] and an array of urls with their corresponding handlers.
Solution
This is a "solved problem", and while it is interesting to implement such a thing by yourself, I would recommend simply to use a library that does it for you. I personally use Fast-Route, a pretty straightforward library that allows for customization in the way you handle routes, but if you google for "php routers" you will find plenty of them.
Of Filename/Methodname
(opinions follow from here on)
This point should be rethinked. While with psr-4 (and psr-0, and probably psr-whatever) the correspondance between a specific class and its file is that the file is named after the class it contains, I believe it better to not think about this as filename/methodname but rather section/action, or whatever speaks best of what the url actually does.
Moreover, if you start using namespaces (which you should do if your oop code becomes slightly more complicated than a hello world page), you obviously won't pass full namespaces in urls, and they actually are irrelevant to your users.
I know this question has been asked numerous times but couldn't find the definitive solution to what I am trying to do.
I am trying to serve multiple apps using single codeigniter 3 installation simultaneously. Specially frontend, backend and other vendor specific interface. This is required because each app has its own login and need to maintain its unique session.
I have gone through codeigniter 3 manual
https://www.codeigniter.com/userguide3/general/managing_apps.html#running-multiple-applications-with-one-codeigniter-installation
I have setup my app this way as suggeted
appfrontend
appbackend
As suggested, I have two index files which routes to each application.
index.php -> appfrontend
admin.php -> appbackend
As suggested in documentation
https://www.codeigniter.com/userguide3/general/urls.html#removing-the-index-php-file
My ".htaccess" file have
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
This removes the index.php for frontend and that's fine.
Question: How to remove admin.php as well from the URL?
I am restructuring a website to a MVC framework and am in the process of moving everything from root the root directory into an organized file structure. So now instead of going to domian.org/homeloans.php, the users will need to do domain.org/loans/homeloans. Here is the .htaccess file I use to direct website traffic.
RewriteEngine on
DirectorySlash on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?rt=$1 [L,QSA]
Shouldn't this be as simple as adding:
Redirect 301 /homeloans.php https://domain.org/loans/homeloans
after turning the rewrite engine on? However, when I do this, I get this in my address bar along with a 404: https://domain.org/loans/homeloans?rt=homeloans.php
Please advise.
The problem seems to be not with the rewrite, but with the ability of your new MVC site to handle this URL. It looks like it can handle:
/loans/homeloans
but not
/loans/homeloans?rt=homeloans.php
Try accessing both directly; if the latter doesn't work but the former doesn't, then you know the issue is with the routing configuration of your MVC application.
I have no idea how htaccess works, and the theory of it just wont connect in my head no matter how many tutorials I read.
I am building a simple MVC framework which works beautifully, except I don't like the way I am dealing with htaccess. To rewrite the URL's properly, this is what I am doing:
RewriteEngine on
RewriteRule ^users/([^/]+)(/([^/]+))?$ controller/users.php?method=$1¶m=$3
If I add a new controller, I then have to go into htaccess and add a new line:
RewriteRule ^access/([^/]+)(/([^/]+))?$ controller/access.php?method=$1¶m=$3
Is there a way to make it all automatic with wildcard fields so I don't have to access htaccess every time I do an update?
You can move logic for parsing query string into your framework/application. For this, make you rewrite rule like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
In this case, any request to server will be processed by index.php (if static file with same name not exists). And $_SERVER['REQUEST_URI'] will be equal real request uri - just parse it and use for your logic.
For example, if send /user/registry request with that .htaccess
$_SERVER['REQUEST_URI'] => '/user/registry'
You can try this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)(/([^/]+))?$ controller/$1.php?method=$2¶m=$4
The two extra rules will skip the rewrite if the file or directoy referenced actually exists on the disk, eg: it won't try to rewrite requests for http://site.com/images/logo.jpg.
I'd redirect all URIs to index.php and allow another well established MVC concept handle the controller dispatching: Routers.
Many (most) MVC (and some non-MVC) applications use this by default because it allows advanced routing techniques (not only controller/action structured URIs).
Controllers can "register" (new) routers and set their priorities. The application can run all routers (in order of priority) until one router finds a matching route (and is able to discern which controller should be used).
For example many blog-like applications will need SEO friendly URIs meaning something like category/subcategory/subsubcategory/blog-article.html. Many cms-like applications will need the same for their hierarchical pages: top-level-page/mid-level-page/low-level-page.html. As well as many eCommerce applications will want that for their products: category/subcategory/product.html.
The above URIs need a router which will check the database to find out which article/page/product has that URI-key.
Recently I created an application (sales portal) in PHP for a small company. The company had a website developed in word press. Since I hadn't worked with word press the way I embedded my application in the website is I simply created a sub directory on the host and uploaded my application there. E.g:
domainname.com - their website
domainname.com/portal - is where my application is placed (the 'index.php' file).
Since a month I am learning Zend Framework 1.8 and I want to rewrite the portal in Zend framework since I wrote the portal from scratch and its core is not as secure as it would be if it implements the Zend framework.
My question is can I include the Zend framework application into the wordpress website the way I did it with 'from-scratch' application, by creating a sub directory on the host and upload the application there? And if yes how should I configure the Zend application so that it recognizes 'domainname.com/portal' as the domain name (as home directory).
The problem that I face right now is that when I type http://www.domainname.com/portal/sales it returns 404 because there is not such directory on the server. Of course what I mean with the above mentioned link (domainname.com/portal/sales) is:
site: domainname.com/portal
controller: sales
action: index
I tried 'domainname.com/portal/index.php/sales' but when someone opens the portal with this link 'domainname.com/portal/' the next linked that is clicked (e.g. domainname.com/portal/sales) shows 404.
(note: the website http://www.domainname.com should be accessible also)
Thanks in advance.
I believe that wordpress ues a .htaccess to redirect all URLs to its index.php.
This is the .htaccess I pulled out of a test installation:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L]
</IfModule>
In your case, you would need to modify the .htaccess file so that it does not care about what's in http://domainname.com/portal.
In your case, your .htaccess file could look at this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} "/portal/"
RewriteRule (.*) $1 [L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L]
</IfModule>
Basically, what this does is that it ignores the subfolder 'portal' and doesn't process it with any of the normal rules.
However, there might be a down side: If future versions of wordpress updates the .htaccess file, then you will need to make those changes to the file again.
[C]an I embed the Zend framework application into the wordpress website the way I did it with 'from-scratch' application, by creating a sub directory on the host and uploading the application there?
Yes you can.
[H]ow should I configure the Zend application so that it recognizes domainname.com/portal as the domain name (home directory)[?]
Sure, please see Base Url and SubdirectoriesÂDocs how you need to configure your request/routing for your Zend Framework based application.