Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

.htaccess files

I have seen .htaccess files in many projects i heard that that helps a lot to make urls more cleaner and to handle get variables more friendly for eg /index.php makes /index and /index.php?pg=4 to /index/4. how to make so is there any tutorial on that at treehouse?

3 Answers

Tobias Karlsson
Tobias Karlsson
2,751 Points

Hi,

What you are talking about have nothing to do with the .htaccess file in general. The Apache web server supports configurations to be made outside of the main configuration file and that should be done in the .htaccess file. If you have access to the main configuration file you should not be using the .htaccess file. This is from the documentation:

In general, you should only use .htaccess files when you don't have access to the main server configuration file. There is, for example, a common misconception that user authentication should always be done in .htaccess files, and, in more recent years, another misconception that mod_rewrite directives must go in .htaccess files. This is simply not the case. You can put user authentication configurations in the main server configuration, and this is, in fact, the preferred way to do things. Likewise, mod_rewrite directives work better, in many respects, in the main server configuration.

When it comes to clean urls you should be using the mod_rewrite module. Enable that and you will have the option to rewrite your urls. Just google on it and you will find a lot of tutorials on the subject.

This is only for the Apache Web Server though, if you are using Nginx or Node there are similar ways to make this happen.

But Regex gives us some cool stuffs when combined with .htaccess i just want to learn those RewriteEngine On, RewriteBase kind stuffs.

Tobias Karlsson
Tobias Karlsson
2,751 Points

But Regex gives us some cool stuffs when combined with .htaccess i just want to learn those RewriteEngine On, RewriteBase kind stuffs.

Sure, I understand, but I just wanted you to understand that you do not need to do it in an .htaccess file. .htaccess files are usually used when you do not have access to the server configuration, a typical place would be in a web server hosting company where they have multiple clients where they can't let all clients access the main configuration file.

So saying when combined with .htaccess is somewhat confusing. It is like saying when combined with configuration. You will always have to configure the rewrite rules for the rewrite module whether it is in a .htaccess file or in any other apache valid configuration file.

That said here is a tutorial on rewrite rules