Jason Behtel
Jason Behtel

Reputation: 35

php and url rewrites

so I was wondering if something like this is at all possible.

I was hoping to change /?directory=$variable to something like /$variable

An actual example might be easier to understand. So I want <a href='/{$variable}' > to really be /?directory=$variable or in url rewite terms i guess /?page=$1

I hope this makes sense. I am really no good with rewriting. Is this even possible? I was hoping to exclude index.php on that. however if that cannot be done, and example with index.php?ect.. will work. Thanks!

Upvotes: 1

Views: 110

Answers (4)

dynamic
dynamic

Reputation: 48131

Yes it's possibile if your server/hosting supports it you can do it by creating an .htaccess file with:

RewriteEngine On
RewriteRule ^/(.+)$ /?directory=$1 [L]

If you want to allow other directories/files to get accessed normally you should do something like:

RewriteRule ^/([a-z]+)$ /?directory=$1 [L]

This will capture any subdirectory like /abc but not /css.css or /customsub/other file

Welcome to the world of SEO optimizer lol.

Upvotes: 1

Lawrence Cherone
Lawrence Cherone

Reputation: 46620

RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ index.php?directory=$1 [NC,L]

Upvotes: 0

Matt S
Matt S

Reputation: 26

URL Rewriting is done on the Web Server (Apache, IIS, etc.) and not within a scripting language like PHP.

If you are using Apache, check this out:

http://httpd.apache.org/docs/2.0/misc/rewriteguide.html

Upvotes: 0

Related Questions