5

I have a URL that will be something like example.com/index.php?p=test so that the PHP will load the test variable using $_GET['p']. The URL can already be simplified to example.com?p=test; however, I wish to simplify this in my .htaccess to site.com/test. How would i go about doing this?

1 Answer 1

13

Place the following in your .htaccess file:

RewriteEngine on

# The two lines below allow access to existing files on your server, bypassing
# the rewrite

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule (.*) index.php?p=$1 [QSA]

You can then access whatever from example.com/whatever like the following:

$value = $_GET['p']; // "whatever"
Sign up to request clarification or add additional context in comments.

8 Comments

I have other files on my server though, using this doesn't allow me to access any of them. For instance, i have site.com/forum, which this breaks :( any way I could possibly have it work ONLY on the root, and only if the directory doesn't already exist?
@hetoan2: I've updated my answer to hopefully help you out with that.
thanks that fixed my directory issue, but I can't seem to access the files from example.com/whatever when i enter it. only when i enter example.com/index.php?p=whatever will it rewrite the url as example.com/whatever
@heotan2: The files must exist/not exist (depending on what you're trying). The above .htaccess should only rewrite to the index.php if the URL specified is not a file/directory on your server.
would you mind telling me how to turn example.com/test into example.com/?p=test; given that example.com/test is not already a directory on the server?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.