Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/158402-odd-question/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/158402-odd-question/#findComment-835439
Share on other sites

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)

Link to comment
https://forums.phpfreaks.com/topic/158402-odd-question/#findComment-835458
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/158402-odd-question/#findComment-835460
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/158402-odd-question/#findComment-835472
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.