N-Bomb(Nerd) Posted May 16, 2009 Share Posted May 16, 2009 I'm trying to use a new method on one of my websites, and instead of using http://example.com/index.php?id=3 I would just like to be able to access that by doing http://example.com/3 Only way I know of doing that is using a ton of different directories which isn't the method I want to take. So what would be the best method of going about this? I mean obviously I need to be able to pull the value with php, that's why I'm posting this in the php section. Quote Link to comment https://forums.phpfreaks.com/topic/158402-odd-question/ Share on other sites More sharing options...
Michdd Posted May 16, 2009 Share Posted May 16, 2009 You'll need to use mod_rewrite Quote Link to comment https://forums.phpfreaks.com/topic/158402-odd-question/#findComment-835388 Share on other sites More sharing options...
briant Posted May 16, 2009 Share Posted May 16, 2009 I believe you'll need to create a .htaccess file. Allow it to redirect all /DIRECTORYID to /index.php?id=DIRECTORYID And exclude ID names such as images, etc. Google search htaccess and mod_rewrite as Michdd has mentioned. Quote Link to comment https://forums.phpfreaks.com/topic/158402-odd-question/#findComment-835401 Share on other sites More sharing options...
The Little Guy Posted May 16, 2009 Share Posted May 16, 2009 If you place this in an htaccess file, it should work: Options +FollowSymlinks RewriteEngine On RewriteRule ^([0-9]+)$ index.php?id=$1 next, in your index.php file, just add this to see if it works: <?php echo $_GET['id']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/158402-odd-question/#findComment-835415 Share on other sites More sharing options...
N-Bomb(Nerd) Posted May 16, 2009 Author Share Posted May 16, 2009 If you place this in an htaccess file, it should work: Options +FollowSymlinks RewriteEngine On RewriteRule ^([0-9]+)$ index.php?id=$1 next, in your index.php file, just add this to see if it works: <?php echo $_GET['id']; ?> I've never worked with mod_rewrite, however I created what you said above exactly and I'm getting a 404 error.. don't believe the mod_rewrite is working as it should. Quote Link to comment https://forums.phpfreaks.com/topic/158402-odd-question/#findComment-835439 Share on other sites More sharing options...
Michdd Posted May 16, 2009 Share Posted May 16, 2009 If you place this in an htaccess file, it should work: Options +FollowSymlinks RewriteEngine On RewriteRule ^([0-9]+)$ index.php?id=$1 next, in your index.php file, just add this to see if it works: <?php echo $_GET['id']; ?> I've never worked with mod_rewrite, however I created what you said above exactly and I'm getting a 404 error.. don't believe the mod_rewrite is working as it should. You'll have to make sure that Allowoverride is set to 'All' for the directory where you're placing the .htaccess file. You can change this in your httpd.conf file. You'll also need to make sure that you have mod_rewrite on. To check to see if it's on create a new php file and just put this inside it: <?php echo phpinfo(); ?> Then use your browsers search feature and search for "mod_rewrite" if it's not found then you don't have it enabled. You have to goto your httpd.conf file and uncomment the lines that load mod_write (and restart your server, obviously) Quote Link to comment https://forums.phpfreaks.com/topic/158402-odd-question/#findComment-835458 Share on other sites More sharing options...
Daniel0 Posted May 16, 2009 Share Posted May 16, 2009 If mod_rewrite wasn't enabled then he would get a 500 error response code, so that's not the problem. It's more likely to be because .htaccess files is disallowed. If you have access to it, it would be performance-wise be better to make the changes in httpd.conf instead of in a .htaccess file. Quote Link to comment https://forums.phpfreaks.com/topic/158402-odd-question/#findComment-835460 Share on other sites More sharing options...
N-Bomb(Nerd) Posted May 16, 2009 Author Share Posted May 16, 2009 Well I actually have shared hosting with 1and1, and according to their FAQ I'm not allowed to access the httpd.conf. They stated in the FAQ that I'm able to use a .htaccess file to change whatever I would like and that would overwrite the httpd.conf. Whenever using mod_rewrite and/or the rewrite engine, it MAY not function properly on standard shared hosting and/or managed servers without the following line added before it in the .htaccess file: Options -MultiViews I tried doing what they suggested in the FAQ, and that was to no avail. Edit: I added "RewriteBase /" into the .htaccess and now it seems to work, not sure what damage that could cause but the rewrite is working nonetheless. Quote Link to comment https://forums.phpfreaks.com/topic/158402-odd-question/#findComment-835472 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.