I am creating URLs in my website as
http://example.com/register/
http://example.com/login/
I am creating above URLS by writing following rules in htaccess
RewriteRule register/ /register.php
RewriteRule login/ /login.php
Now its working fine for me, but if I create my URLs by creating seprate folder for /login/index.php and /register/index.php By creating folders and index.php files inside those I can achieve above functionality easily.
But I want to know which w=one will be faster, I tried both methods but not seen much difference, according to logic and apache specifications and everything which method will be faster and a good method to go with.
My friend says .htaccess rules will be slower, because in case of htaccess first it will check for rules and then it will redirect to the corresponding attached page, this process will take time than folder organization.
I know that .htaccess will at least be easier for development/debugging purposes because:
1) Because all of your files will be in the same directory instead of their individual ones.
2) Because your editor will show the actual filenames instead of multiple index.php.
Also, since you're using .htaccess, this makes it easier to put a rule in such as:
RewriteRule ^getdata/(\d+)/$ getdata.php?page=$1
Which you can't do when using the directory-based methods.
Overall, the speed difference is negligible compared to the benefits available from using mod_rewrite.
.htaccess will be slower, but the time difference will be negligible.
You should concentrate on other things - like which one is easier to maintain.
Related
I recently asked a similar question and received some great answers, but I'm realizing that it wasn't quite the right question.
We currently use a CMS that fulfills 95% of our needs on our site, however for that extra 5% I'd like to bring in a PHP Framework for things like Form Processing/Validation, Authentication, etc. My original question was if it would be possible to have a single sub-directory within our site that could manage all of those projects. But now I'm realizing that this method would need everything to be in something like a .../projects URI, breaking a pretty good REST setup we have now with the CMS (the only routing I'd want to have to manage is within my own applications, not the entire site to work around this).
Is it possible to have Laravel's main folder at the root directory, and only include it in specific sub-directories or files that I want. I imagine it something like just having multiple public folders, which I'm not sure if it's possible/reccommended? I also imagine I would have to essentially turn of Laravel's Routing so that it doesn't step on the toes of the CMS, but I believe I could turn it back on for each folder with each having its own .htaccess?
I picture the architecture looking sort of like this:
root
/ \
html laravel
/ \
... [normal page]
/ \ \
/ \ [project]
[project] [normal page]
I've read a little that the Zend Framework is made especially for partial integration like this, in which case if it's better suited for the job, I could definitely switch over. But it'd be great if it'd be possible with Laravel since I'm already fairly comfortable with the basics.
You should be able to do this fairly easily. The basic steps include:
Modify your .htaccess or vhost files so that each location hosted by Laravel points to Laravel's public/index.php. You'll need to put these more specific rules first, as Apache will follow the first matching rule that it finds.
[Optional] If you want to change the folder structure of your Laravel project, modify the files in the bootstrap folder (paths.php, autoload.php, and start.php) to point to the correct paths, and make sure that public/index.php (or wherever you put it) can find the autoload and start files.
That should be it. Make sure that your Laravel routes reference the full path.
Unfortunately, this approach does mean you'll need to add a rewrite rule for each Laravel route you want to add. This way, Laravel won't interfere with your CMS's routing; it will only handle requests that get rewritten to go to index.php. You just have to make sure that the rewrite rules have higher priority by putting them before the general rewrite rules for your CMS.
I realise this is a trivial question but I don't have access to a Joomla installation to test.
I have to write some code that does a bunch of background processing, talks to web services etc and does not relate whatsoever to the Joomla CMS used by this existing site. I know Joomla does a bunch of URL rewriting, what I want to know is if I stash my processing code in say {domain}/tools how easy is it to tell Joomla not to do anything with that? Is it just a matter of adding a rule to .HTACCESS to avoid that directory, or does Joomla spider it and play with its files?
Bare in mind my experience with Joomla is near 0 (I've used it only to maintain some articles on various sites previously) and I'm not doing Joomla work, my stuff just needs to coexist with Joomla on this server.
Joomla! can co-exist with other systems quite easily the main consideration is the way the standard Joomla! .htaccess file (if enabled) remaps everything through the /index.php file for SEO purposes.
However, it shouldn't cause you any issues as the .htaccess file is not supposed to rewrite the request if a real file exists at the request path.
# and the requested path and file doesn't directly match a physical file
RewriteCond %{REQUEST_FILENAME} !-f
If the .htaccess catches your /tools/ example then yes you would have to modify the default configuration.
(For completeness, Joomla! also ships with a web.config file for MS servers that does a similar remapping of requests).
I created a website using php, passing values from page to page either with POST or GET.
Though there is some cons, I dont know how to track specifically what data has been viewed in GoogleAnalytics as it just shows the name of the page (xxxx.php)
On the other side, I see websites structured differently with a bunch of subdirectories created : www.xxx.com/xxxxxx/xxxxx/xxx
This looks like pretty manual for me , compared to the .php?xxxx=xxxx way of structuring.
Do you know how this subdirectory style structuring can be automatically obtained?
This is done with Apache rewrite rules.
To make it so that whenever a user goes to /posts/visiting_new_york, it actually goes to to /viewpost.php?id=visiting_new_york, you create a file in your site called .htaccess like this:
RewriteEngine On
RewriteRule '^posts/([^/]+)$' viewpost.php?id=$1 [L]
Use an MVC framework like rails, or simply configure your webserver's virtual directory structure to be identical to the local servers file system and adhere to that scheme when saving your php files.
Yes, you can do this with "mod_rewrite" in apache.
It involves creating a .htaccess file with URL re-writing rules inside.
So you can transform /index.php?page=contact&lang=en into /en/contact/
Here's a good rewrite cheat sheet: http://www.addedbytes.com/cheat-sheets/mod_rewrite-cheat-sheet/
Wadih
You need to read about url rewriting
http://httpd.apache.org/docs/2.0/misc/rewriteguide.html
If you just want to track your dynamic pages , there is another solution in Google analytic
http://www.google.com/support/googleanalytics/bin/answer.py?answer=55504
I'm new to PHP and web development in general, but have been a programmer for 5 years. I'm trying to work on my own website from scratch using Notepad to edit my PHP files and WAMP for my server. I'm able to view my files fine in Safari, Chrome and Firefox, but not IE (which we all know IE isn't the greatest) because I'm using some HTML5 stuff. Anyways, I have an Includes folder that holds my files for my header, menu and footer. I have an index.php file that includes these files and displays them fine. In the center of the page is where I want the content. To try and keep clean urls, I made quite a few folders and put this same index.php file in there (e.g. Profile/index.php, Forums/index.php, etc.). I did this so when I went to localhost/mysite/profile/ it showed me the template I wanted to use. However, there has got to be a better way to use the template and a better way to have clean urls. I'm not currently hosting this site anywhere so I don't know if I'll have access to the htaccess file (not even sure what it is honestly, just seen it mentioned), but I was curious of having the folder structure (one folder for each menu item) is a normal or ok practice? I was also curious if there is a way to use the index.php without having to copy and paste it every time I make a small change. Any help would be appreciated. Thanks.
If you are planning on templating I suggest using an existing platform like Symfony, Zend Framework, or Smarty. Smarty is probably the easiest to get going with and it is (almost) purely for templating. Whereas Symfony and Zend Framework are big packages with lots of bells and whistles.
If I was going to be doing pure templating I would look at Smarty. I use Zend Framework for just about all my current PHP projects now but it has a pretty steep learning curve. Your first couple weeks will be frustrating.
As far as URLs go, .htaccess is probably the preferred method (at least in my book). Zend Framework and Symfony both have kind of default URL writing style that looks like http://host/controller/action where controller would be Profile or Forums. You wouldn't necessarily have to see host/profile/index, it could be host/profile or host/profile/edit, where edit is the action being performed.
Copying the index.php file is not really the way I would go. It seems messy and there are a few other options. If you want the urls to be clean and search engine friendly you should really have a look at url rewriting using .htaccess
You're saying that you're not sure if you will have a server with "access to the htaccess file" but if you can upload files you can always upload a .htaccess file as well -- the problem is that the web server is not always using them or might not have mod_rewrite enabled. In this case you can get your urls on the format of http://www.example.com/index.php?u=/Profile/foo (this is also my preferred way to handle url rewrites).
In your index.php just make sure to read the requested url parameter passed by mod_rewrite and include whatever files in the folder that you need. Actually, you don't even need a "physical" folder structure, you might want to implement it using classes or something like that. (I'm sure I would).
I would really recommend that you go have a look at a good PHP framework, CodeIgniter for example. Even if you decide to code everything from scratch, you would still learn a lot about best practices for url handling, databases, the MVC pattern, template engines, database abstraction layers and PHP coding in general.
your answer is the htaccess file, is converts the 'folder structure' to $_GET value's
for example,
if you're in website.com/Profile/ you can write an htaccess line that will convert that into website.com/index.php?folder=Profile
htaccess:
RewriteEngine on
RewriteRule ^/(.*)/$ index.php?folder=$1 [NC,L,QSA]
RewriteRule ^/(.*).html$ index.php?folder=$1 [NC,L,QSA]
Hello stackoverflow community,
I've got a little Problem with my CodeIgniter.
I use two applications in my app folder. The first one is the frontend, the second one the backend.
In my root directory I've two .php files. An index.php leading to the frontend directory and a backend.php leading to the backend directory.
Since I use mod rewrite in order to get clean URL's there is a problem with that.
The URL structure is the following: www.domain.com/controller/action
That's the action of the controller in my frontend Application.
I use htacces to get rid of the /index.php/ between domain and controller.
To access my backend application I want my URL to be like this www.domain.com/admin/controller/action
therefore I have this rewrite rule:
RewriteCond %{REQUEST_URI} ^admin.*
RewriteRule ^admin/(.*)$ /backend.php?/$1 [L]
Now the problem:
CodeIgniter assumes that /backend/ is the first URI segment, and wants to treat it mistakenly as my controller.
Do I really have to edit the core of CodeIgniter in order to tell it not to use the Server Request URI or is there another trick?
Thanks in advance,
Thomas
This really isn't going to work. Check out config/config.php and config/settings.php etc for constants that need setting... You really need to run each application in a separate 'application' directory. It's also worth asking yourself if they even need to be 'separate' applications...
Basically, stop going down this path while you still can, it will lead to heartbreak, and reams of horrible code.
Instead of having two application folders I would suggest having a folder within the controller called 'admin', you could also have folders within your models, views (even libraries) named 'admin' which you could then pull out of this project and move to other projects.
This would allow you to just use a standard rewrite to remove the index.php from the URL, and domain.com/admin would point to your admin folder within the controller.
You just need to be sure not to have a controller within the root controller folder named 'admin.php' or you will run into issues.
For more information take a look at: Organizing Your Controllers into Sub-folders within the CI docs.
Also note that an out of the box MVC framework might not always be best used for creating a CMS. Typically an MVC framework is best used to quickly create web applications which have fairly static routes pointing to specific controllers. CMS's on the other hand tend to give full control over the website, which leads to database driven routes which often eliminate the need for typical controllers unless you have a heavily modified routing system.