简体   繁体   中英

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!

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.

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

RewriteEngine On

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM