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
Related
I'm trying to change the name from my url's to for example home instead of index.php. I've read you can do that with the .htaccess file but for that you need Apache stuff and I don't have anything like that.
Isn't there any more easy way to change the url?
Yes, there is an easy way to change the URL without using the .htaccess file. This can typically be done by using a web framework or a CMS (Content Management System) such as WordPress, Laravel, or Django. These frameworks and CMSs have built-in functionality for handling URL routing, which allows you to define custom URLs for your pages.
Another way is to use a URL rewriting module provided by your web server software such as IIS URL Rewrite for IIS web server, or mod_rewrite for Apache. These modules allow you to rewrite URLs on the server side without requiring changes to the actual file structure on the server.
Without more information about your web server, it's difficult to give more specific instructions. But you could check the documentation or the community forum of your web server or the language framework you're using to find more information about how to change the URLs in your application.
I have a site with this structure:
Home
About Us
Products
Product
Services
Contact
If I open
mysite.com/products
reads page-products.php
and
mysite.com/product
reads page-product.php
I need to use page-product.php as a generic page.
For Example...
mysite.com/product/The-Best-Product/32
read page-product.php?name=The-Best-Product&id=32)
And how about
mysite.com/john/
and reads page-user.php?username=john
Its possible to make something like that works?
Thanks!
Consider using the Apache Web server's mod_rewrite, which is a very powerful tool to do just what you need. This would only help you if you are using Apache. Also, if you are not the Webmaster, mod_rewrite must have been enabled for use in .htaccess files.
The bottom of the page I linked has some examples on how to use this. Also, there are many tutorials around.
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]
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.
I have a simple membership project and teh client wishes that urls be simple rather than having something like:
index.php?p=view-member&id=568157&hash=00218546598797898798162454712
he would like to see it as so:
website.com/member/username
Now I've worked with Zend and understand you need to do some htaccess alterations to get this done and this is like already easy to do in Zend Framework but I don't want to use the Zend framework for this as I already have a fully functional membership project I built which I would like to tweak for this project.
I thought of creating empty member folders with a simple php file that would include my main php file but am not sure of what complications I would run into if I use such a system. I'm open to ideas here guys. What do I do?
You don't need a framework to do this. Just use Apache URL rewriting with mod_rewrite. For example, in .htaccess in your document root (top-level directory that is served):
RewriteEngine On
RewriteBase /
RewriteRule ^member/(\w+)$ /index.php?p=view-member&id=$1
If you need some sort of hash to identify the user, don't put it in the URL (like your hash parameter is). Use a cookie.
You can of course use other Webservers such as IIS or nginx.
If you are running Apache a RewriteRule with mod_rewrite could do the trick. You also have to change index.php so that it takes username as a parameter and stores the hash in a cookie/session instead of in the URL.