N-Bomb(Nerd) Posted July 20, 2011 Share Posted July 20, 2011 Hi, I'm have my website and I'm trying to create links to use my php page called us.php, but I want to have neat pretty links for it.. much like this: http://www.example.com/us/neat-awesome-video Obviously the variable there would be "neat-awesome-video" and I want that passed into us.php.. how could I accomplish this? Also, what if I wanted to pass in a second variable.. how would I go about that? Quote Link to comment https://forums.phpfreaks.com/topic/242410-removing-php-in-url-with-php-get-vars/ Share on other sites More sharing options...
ZulfadlyAshBurn Posted July 20, 2011 Share Posted July 20, 2011 use mod_rewrite using htaccess. edit your htaccess check this site: http://corz.org/serv/tricks/htaccess2.php Quote Link to comment https://forums.phpfreaks.com/topic/242410-removing-php-in-url-with-php-get-vars/#findComment-1245043 Share on other sites More sharing options...
JonnoTheDev Posted July 20, 2011 Share Posted July 20, 2011 You would use Mod-Rewrite. There is a whole board on this topic within this forum. So to get to http://www.example.com/us/neat-awesome-video you must start with your original URL. This may look like: http://www.example.com/us.php?title=neat-awesome-video Now you can enable mod-rewrite in a .htaccess file that you upload to the website document root and add the url rewrite rule. # .htaccess file contents # enable mod-rewrite RewriteEngine On RewriteBase / # add rule for us.php RewriteRule ^us/([a-z0-9-]+)$ us.php?title=$1 [L] Now your url will work when it is seo friendly. This part ([a-z0-9-]+) is called a regular expression. In this example it will only accept lowercase letters a-z and numbers, and hypens. Any other characters will fail and throw a 404 error so you must make sure that your parameters contain the correct characters defined in your rewrite rule. If you want to add extra parameters lets say an id that is a numeric value. So, your non-rewritten url will look like http://www.example.com/us.php?title=neat-awesome-video&id=123 We can amment the rewrite rule to: # add rule for us.php RewriteRule ^us/([a-z0-9-]+)/([0-9]+)$ us.php?title=$1&id=$2 [L] The second parameter (id) is captured and can only be a number so now your URL will look like http://www.example.com/us/neat-awesome-video/123 Quote Link to comment https://forums.phpfreaks.com/topic/242410-removing-php-in-url-with-php-get-vars/#findComment-1245046 Share on other sites More sharing options...
N-Bomb(Nerd) Posted July 20, 2011 Author Share Posted July 20, 2011 I've been reading up and trying everything I know and I'm unable to get this to work correctly. I'm using 1and1 shared hosting and I know for a fact that they have mod_rewrite enabled, but I can't seem to get this to work at all. Here's what my .htaccess file looks like thus far: AddHandler x-mapp-php5 .php RewriteEngine On RewriteBase / RewriteRule ^us/([a-z0-9-]+)$ us.php?title=$1 [L] When I try to access the page via http://example.com/us/awesome-stuff I get a 404 error and the page doesn't load. My us.php file is located at http://example.com/us.php . Also, does the "www" prefix matter when dealing with these issues? Quote Link to comment https://forums.phpfreaks.com/topic/242410-removing-php-in-url-with-php-get-vars/#findComment-1245323 Share on other sites More sharing options...
JonnoTheDev Posted July 21, 2011 Share Posted July 21, 2011 Your script isn't causing the 404 is it? You must test the none rewritten url first: http://example.com/us.php?title=awesome-suff The www will not cause an issue but as a rule of thumb you should always redirect the non www version to the www version or vica-versa. Search engines used to see this sort of thing as duplicate content when both www and non www urls serve up the same page when people link to your website using both versions. Quote Link to comment https://forums.phpfreaks.com/topic/242410-removing-php-in-url-with-php-get-vars/#findComment-1245518 Share on other sites More sharing options...
N-Bomb(Nerd) Posted July 21, 2011 Author Share Posted July 21, 2011 Your script isn't causing the 404 is it? You must test the none rewritten url first: http://example.com/us.php?title=awesome-suff The www will not cause an issue but as a rule of thumb you should always redirect the non www version to the www version or vica-versa. Search engines used to see this sort of thing as duplicate content when both www and non www urls serve up the same page when people link to your website using both versions. When using the url ( http://example.com/us.php?title=awesome-suff ) to test with it works perfectly.. that's why this is very frustrating to me. Quote Link to comment https://forums.phpfreaks.com/topic/242410-removing-php-in-url-with-php-get-vars/#findComment-1245757 Share on other sites More sharing options...
N-Bomb(Nerd) Posted July 21, 2011 Author Share Posted July 21, 2011 This is basically the last thing I need in order to get my website operational.. would really love to figure this out. Quote Link to comment https://forums.phpfreaks.com/topic/242410-removing-php-in-url-with-php-get-vars/#findComment-1245873 Share on other sites More sharing options...
N-Bomb(Nerd) Posted July 22, 2011 Author Share Posted July 22, 2011 I wish I could get this figured out, because this is all I need for my site to be completed and there's a rather large user base waiting on me to release this. Quote Link to comment https://forums.phpfreaks.com/topic/242410-removing-php-in-url-with-php-get-vars/#findComment-1245998 Share on other sites More sharing options...
JonnoTheDev Posted July 22, 2011 Share Posted July 22, 2011 Try adding symlinks into the .htaccess Options +FollowSymLinks RewriteEngine On RewriteBase / RewriteRule ^us/([a-z0-9-]+)$ us.php?title=$1 [L] Please take the time to carefully read this tutorial. http://www.easymodrewrite.com/ Quote Link to comment https://forums.phpfreaks.com/topic/242410-removing-php-in-url-with-php-get-vars/#findComment-1246059 Share on other sites More sharing options...
N-Bomb(Nerd) Posted July 22, 2011 Author Share Posted July 22, 2011 Try adding symlinks into the .htaccess Options +FollowSymLinks RewriteEngine On RewriteBase / RewriteRule ^us/([a-z0-9-]+)$ us.php?title=$1 [L] Please take the time to carefully read this tutorial. http://www.easymodrewrite.com/ I've already tried adding "Options +FollowSymLinks", but I just tried adding it again and I'm not having any success. I think there may have something to do with my shared hosting company and the "base" option, but I cannot figure that out. Quote Link to comment https://forums.phpfreaks.com/topic/242410-removing-php-in-url-with-php-get-vars/#findComment-1246327 Share on other sites More sharing options...
JonnoTheDev Posted July 25, 2011 Share Posted July 25, 2011 remove RewriteBase / Quote Link to comment https://forums.phpfreaks.com/topic/242410-removing-php-in-url-with-php-get-vars/#findComment-1246715 Share on other sites More sharing options...
N-Bomb(Nerd) Posted July 25, 2011 Author Share Posted July 25, 2011 remove RewriteBase / I was able to get it working using "Options -MultiViews", apparently something is configured differently with 1and1 and that's required. However, I've ran into another problem and I've posted it here: http://www.phpfreaks.com/forums/index.php?topic=339393.0 Quote Link to comment https://forums.phpfreaks.com/topic/242410-removing-php-in-url-with-php-get-vars/#findComment-1246904 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.