簡體   English   中英

php 和 url 重寫

[英]php and url rewrites

所以我想知道這樣的事情是否可能。

我希望將/?directory=$variable更改為/$variable

一個實際的例子可能更容易理解。 所以我希望<a href='/{$variable}' >真的是/?directory=$variable或者在 url 中我猜想/?page=$1

我希望這是有道理的。 我真的不擅長重寫。 這甚至可能嗎? 我希望排除 index.php 。 但是,如果無法做到這一點,例如 index.php?ect.. 將起作用。 謝謝!

是的,如果您的服務器/主機支持它,您可以通過創建.htaccess文件來做到這一點:

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

如果您想允許其他目錄/文件正常訪問,您應該執行以下操作:

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

這將捕獲任何子目錄,例如/abc但不/css.css/customsub/other file

歡迎來到 SEO 優化器的世界,哈哈。

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

如果您使用的是 Apache,請查看:

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

RewriteEngine On

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM