我正在開發一個使用PHP的應用程序,我希望我的用戶能夠使用自己的虛榮URL。使用PHP和htaccess的虛榮URL
在這種格式:
http://example.com/username
例如:
http://example.com/myCoolUsername
如果我用這個URL現在服務器認爲它是一個文件夾,但我知道我應該使用。 htaccess文件的方式,但我不知道我應該在該文件中使用什麼內容。
我正在開發一個使用PHP的應用程序,我希望我的用戶能夠使用自己的虛榮URL。使用PHP和htaccess的虛榮URL
在這種格式:
http://example.com/username
例如:
http://example.com/myCoolUsername
如果我用這個URL現在服務器認爲它是一個文件夾,但我知道我應該使用。 htaccess文件的方式,但我不知道我應該在該文件中使用什麼內容。
此規則將改寫http://domain.com/username
到user.php
與username
作爲GET參數(name
)。
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /user.php?name=$1
要進行測試,創建一個在您的根目錄下的螞蟻命名user.php
文件提出,在你的文件:
訪問http://domain.com/blablaName123
,如果一切正常,你應該看到string(13) "blablaName123"
這裏假設你的index.php
收到$_GET['username']
映射到他們的個性化網址:
RewriteEngine On
# Make sure you only match on files/directories that don't actually exist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite the first part after the `/` into a `username` parameter
RewriteRule ^(.+)$ /index.php?username=$1 [L,QSA]
HTTP: //httpd.apache.org/docs/current/mod/mod_rewrite.html –