1

If I have a directory called "dir" with a file called "index.php", is it possible to handle a string in the URL without any variable specifically defined within the url?

Eg. If the user types http://domain.com/dir/hello.world

Is it possible to handle that 'hello.world' from dir/index.php without it being assumed as a file that does not exist?

I'm assuming this is a htaccess thing, I know how to make variables hidden in a url with htaccess (so like /index.php?var=something becomes /var/something) but I don't know how I'd go about doing it so it becomes just /something.

1 Answer 1

2

You are looking to implement something often referred to as "pretty urls". You can do this by forcing all requests that aren't files or directories to the index.php in your .htaccess:

Options +FollowSymLinks
RewriteEngine On

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

RewriteRule ^.*$ ./index.php

Then you can pull your parameters out of the request uri:

$_SERVER['REQUEST_URI'];
Sign up to request clarification or add additional context in comments.

1 Comment

I'll accept your answer because it's good. Though one thing I changed was using these rules: RewriteRule ^(.*)/?$ index.php?var=$1 [L,NC,QSA] and RewriteRule ^(.*)$ $1.php [L] so everything is passed to var variable. Though your solution is better when there's more than one variable being passed.

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.