1

Before I go ahead and ask you my question, I understand that there are many methods on the internet to accomplish this sort of thing. I understand also that this process is called rewriting urls. However, since I am not a proffesional at coding, the process of these things seem very daunting and hard to understand. So, I thought I'd ask the StackOverflow community for help!


Here's what I'd like. Currently, I have 2 main files.

index.php and php.php.

My site allows users to enter a username and get information for that username. index.php holds the form input, and php.php handles the actual PHP Code. Once a user has submitted the username, the URL at the end will look like this;

http://mcspy.info/php.php?username=_scrunch

What I'd like the URL above to look like is:

http://mcspy.info/player/_scrunch

Could somebody here please guide me and explain in an easy manner to understand, how I would go about doing this? Preferably using PHP! Thanks a lot!

JLWillliamsUK
  • 185
  • 1
  • 3
  • 17
  • first find out how you can handle sending every request except for the request of a file or folder through htaccess to the php – Giovanni Le Grand Mar 17 '15 at 17:44
  • This won't be doable without `.htaccess`, you need to tell your webserver what to serve. –  Mar 17 '15 at 17:45
  • http://phpbridge.org/intro-to-php/pretty_urls – Siim Kallari Mar 17 '15 at 17:48
  • I appreciate it can be daunting to learn these things, but unfortunately that doesn't allow you to bypass "duplicate" rules. What you need to do is try some code, htaccess rewrite rules, and if you cannot get it working, post the code you have, why it doesn't work, what it does, what it should do. Then we can help better. Seriously, try some code. The code is already here on Stack easy to find. It's only code someone will give you here in an answer (as we cannot really give you a "tutorial") – James Mar 17 '15 at 17:50

2 Answers2

3

Well it would be much easier to do this with a .htaccess file:

RewriteEngine On
RewriteBase /
RewriteRule ^player/(.*)$ php.php?username=$1 [L,NC]
Marc
  • 3,683
  • 8
  • 34
  • 48
  • Hm.. this code doesn't seem to do anything, my URL is still the same as it was :( – JLWillliamsUK Mar 17 '15 at 17:52
  • You need to call http://mcspy.info/player/_scrunch the .htaccess file will internally call the php.php and pass everything after player/ to the php.php as the $_GET['username'] – Marc Mar 17 '15 at 17:53
  • @JLWillliamsUK have you enabled the mod rewrite so in php.ini? – Giovanni Le Grand Mar 17 '15 at 17:54
  • I just called ur website and saw that you are running on hostinger. There is a FAQ to rewrite rules: http://www.hostinger.ph/knowledge-base/489.html – Marc Mar 17 '15 at 17:56
  • @Marc How would I do this? I'm not the greatest and experimenting with new things! Also, currently, I'm using a Virtual Host to debug my site at the moment! – JLWillliamsUK Mar 17 '15 at 17:57
  • @GiovanniLeGrand mod_rewrite is enabled within Apache, yes. – JLWillliamsUK Mar 17 '15 at 17:58
  • Well first you have to setup the .htaccess file. According to hostinger, the files is already created in your home directory. So you have to edit it and add the code above and the rewritebase from the FAQ. Then you can just call (in your browser) http://mcspy.info/player/_scrunch and the .htaccess will internally call the php.php ATM you get a 404 when you call http://mcspy.info/player/_scrunch ;-) – Marc Mar 17 '15 at 17:59
  • @Marc ah, I see! Thanks! That seemed simple! Thanks a lot! – JLWillliamsUK Mar 17 '15 at 18:04
1

The easiest and cleanest way is with .htaccess files, but if you don't want to, you can use PHP, what I'm not sure but this would help URL rewriting with PHP

Community
  • 1
  • 1