I'm working on a project using Cloud9 IDE, running PHP5 on Apache 2.0. I'm primarily a front-end developer, but I have decent experience with PHP. I'm familiar with MVC frameworks, and to a lesser degree, this functionality is what I'm trying to emulate, but simpler. I tried implementing CakePHP, but found it was a little too robust for what I needed -- I don't want a backend-heavy setup. I want to write some custom sort of routing mechanism for my application.
Ideally, I would like every request to my site to come through one page (this custom "Controller"), and from there I can write my own logic to figure out the appropriate templates, http codes, errors, etc., to include. My question is, how do I make this happen? In other words, how do I make a request to http://mysite.c9.io/user/view/2 get channeled through http://mysite.c9.io/index.php , and not try to request the /user/view/2 directory on my server?
I'm vaguely familiar with mod_rewrite and .htaccess rules, but I suspect they may play a role here.
First make sure that mod_rewrite is enabled. Check your httpd.conf file for
LoadModule rewrite_module modules/mod_rewrite.so
Make sure it's not disabled with a # in front of it. Next change the root <Directory> settings to
<Directory />
Options All
AllowOverride All
</Directory>
Make sure you change all the occurrences of AllowOverride None to All. Then restart Apache.
Now that mod_rewrite is enabled, add this to an .htaccess file in your web root / directory
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !^/index\.php$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d # not a dir
RewriteCond %{REQUEST_FILENAME} !-f # not a file
RewriteRule ^ index.php [L]
This makes sure that every request that otherwise would have been a 404 (which means it excludes images, css, js etc.) now routes through the front controller index.php. Some, content management systems like to add another %{REQUEST_URI} check to make sure index.php is only invoked to process the kind of requests the framework actually expects.
Joomla, for example, adds the following:
RewriteCond %{REQUEST_URI} /component/|(/[^.]*|\.(php|html?|feed|pdf|vcf))$ [NC]
Related
It seems I am stuck. I have found answers, but I can't get them to work.
I am working on this website. I use a switch to determine what content should be visible. I use the variable $page.
The links with this method are not good for SEO so I want them to be /example instead of index.php?page=example.
I have looked at all the different answers to this on StackOverflow, but I can't get any solutions to work. There are no errors coming up and the site will show just fine, but the rewrite doesn't work.
I have tried on both my servers on servage.net and one.com
Any suggestions? :D would be much appreciated!
Try adding this to the .htaccess file in your web document root folder (often public_html or htdocs), then point your browser to example.com/somepage:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^./]+)/?$ page=$1 [L,R]
Once you are satisfied that the redirect works, you can change the R to R=301 to make it permanent.
This assumes that mod_rewrite is both installed and activated for htaccess files.
If you are not sure, to check if mod_rewrite is installed, look at the list of installed modules in the output of phpinfo();
By default, mod_rewrite is not enabled for htaccess files. If you are managing your own server, open httpd.conf
and make sure that the webroot directory block contains one of these lines: AllowOverride FileInfo or AllowOverride All
I am trying to learn fuelPHP and I came form a CI background. I am using this tutorial so that I can familiarize myself on this.
http://net.tutsplus.com/tutorials/php/getting-started-with-the-fuel-php-framework/
My problem is in step 2. When I access my simple controller
http://localhost/fuel/public/index.php/hello
I get a 404 error.
*fuel is the directory of this freshly installed fuelPHP on my localhost.
When I set this to be my default route, the controller works.
Did something change in the new version that prompted this?
Here is my .htaccess on the /public/ folder.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
Thanks.
You are making it very complicated for yourself by installing FuelPHP like this.
It has been designed in such a way that the 'public' folder is your webservers document root, which means all fuel stuff is outside your document root, and the .htaccess is directly inside your document root.
By not only installing it entirely inside your document root, but also in a subfolder of that document root, you need to modify your rewrite rules. Start by adding
RewriteBase /fuel/public
to the one in public, and see if that helps. Also, this .htaccess is made for "standard" apache type installations. If you run anything else you might need a different .htaccess. If you use fcgi for example, get the .htaccess from the 1.4/develop repo, it's more fault tolerant towards your webserver type.
Final remark: you should not use localhost as hostname. It's an illegal name in a lot of RFC's, one of them being the one that documents the use of cookies. You'll have all kinds of session issues with some browser (versions) if you use "localhost". Instead, setup virtual hosts (a better solution then subfolders anyway), and use a hostname like "mymachine.local".
I'm working on a web page project. I decided to use Apache, PHP (5.1.7, version imposed by my service provider) and Dwoo (templating) for this purpose.
I want to route URLs to my templates. I'm aware there are many frameworks doing this kind of thing. I'm just wondering if there's a nice way to achieve it without.
I've set up my project as follows:
src/dwoo - Dwoo files
index.php - This should handle routing. Currently it just renders the front page of the site using a template.
templates - Templates that represent actual pages.
There is minimal amount of business logic (no real model). It's all just pretty static pages. Using templates makes maintenance work easier (inheritance ie.).
Any idea how to set up routing in this case? I guess ideally each given URL should route via index.php that then somehow decides which template to render (ie. /category/pagename would map to templates/category/pagename.tpl).
Use mod_rewrite to route everything to a single index.php file. Then check the variable in $_SERVER['REQUEST_URI'] within this file to dispatch to the required handler.
This configuration will enable mod_rewrite, if it's installed:
DirectorySlash Off
Options FollowSymLinks Indexes
DirectoryIndex index.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^.*$ - [L]
RewriteRule ^.*$ index.php [L]
Like trolskn (+1) describes:
Use mod_rewrite to route everything to a single index.php file. Then check the variable in $_SERVER['REQUEST_URI'] within this file to dispatch to the required handler.
But I found the following .htaccess (placed in the folder with the index.php which should "consume" everything after it) much more helpful:
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
Also I would like to note that you might encounter the message
.htaccess: Invalid command 'RewriteEngine', perhaps misspelled
or defined by a module not included in the server configuration
This can easily be solved with sudo a2enmod rewrite && sudo service apache2 restart (source)
You might want to use PEAR's Net_URL_Mapper.
Generally a url that looks like this:
http://www.domain.com/product.php/12/
will open up product.php and serve the /12/ as request parameters, which then my PHP script can process to pull out the right product info. However when I migrated this whole site, after developing it, to a new server, I get a 404 error, because on that server it's not defaulting to the mother directory/file in case of an absence of requested directories.
I vaguely remember learning that this is generally a common apache function but I can't seem to recall how to set it up or how to manipulate it.. if there's an .htaccess method to achieve this that would be great.
What you're referring to is mod_rewrite. The official docs for it are here: http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html
You would configure it either in your VHost definition (recommended) or in an .htaccess file.
Assuming that you want to map all requests to a resource that Apache cannot serve (such as files that don't exist) to products.php you can use the following:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*)$ /products.php?request=$1 [NC,L]
You can then use $_GET['request'] to get the path requested and take it from there, depending on what you want to do. I'd normally recommend letting mod_rewrite handle parsing the request and passing the proper attributes to your PHP, but if you're not familiar with mod_rewrite it's probably easier to do it in your PHP.
you can use mod rewrite engine to map this to
http://www.domain.com/product.php?arg=12
Mod rewrite details: http://forum.modrewrite.com
Sample:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^files/([^/]+)/(.+) files.php?app=$1&file=$2 [NC]
this rewrite rule will map any request containing files/firstrPart/secondpart to the script files.php
everything between the first and second slash after files will be passed as parameter app and the rest as file
Basicly you define a regex with some subpaterns and state which script should really be called.
You cna refer to the subpatterns with $n where n is the 1 based index of the pattern.
Have fun.
NOTE this is a extreme simplification of mod rewrite. Please do some research before you use it because this might go terribly wrong...
The directive you're looking for is "AcceptPathInfo on". mod_negotiations MultiViews feature would also give you the option of not including the ".php" which is another common one people abuse mod_rewrite to do.
I have a content site that spreads across multiple pages but there is only 1 index.php file which retrieves the data from the database based on the page no.
Currently the url direction has to be something like domainname.com/Page/content.php?page=3
I have noticed, quite a few sites have directory like structure for the urls like:
domainname.com/Page/3
I know how to change it to domainname.com/Page/?page=3 but I am looking to remove the ?page part.
How can I do this, without individually creating a directory for each page, the content keeps growing, hence changes for each page.
Thanks
These sites use mod_rewrite to do a "rewrite" on the requested URL using a regular expression
EDIT:
rewriting this: domainname.com/Page/3
to that: domainname.com/Page/content.php?page=3
would look like this in the .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z]*)/(.*)$ /content.php/$1/page=$2 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /error_with_mod_rewrite.html
</IfModule>
here i also made a restriction for the var name to be a letter either capital or small.
"URL Rewrites" is the feature that performs a custom URL structure. This is a very common and important feature in most common web servers. URL rewrites are not just available with Apache. For example,
Apache uses the Mod_Rewrite Module.
Nginx uses the 'Rewrite' and "Try_Files" directives.
Cherokee has the feature built-in, configurable from admin web panel.
there are many more examples, these are just some I have worked with. In addition, I have heard some projects do the rewrites in the programming code. This has become more prevalent with the new language specific web-servers.