簡體   English   中英

在htaccess中使用重寫的友好URL

[英]Friendly URLs using rewrite in htaccess

我有這個網址

http://mysite.com/profile?id=78

我想改變它是這樣的

http://mysite.com/78/

我該怎么辦?

我在我的htaccess中有這個代碼,但它似乎無法正常工作。

RewriteRule ^id/([a-zA-Z0-9]+)/$ profile?id=$1

請指導我這個。

您的幫助將受到極大的贊賞和獎勵。

謝謝!

我很確定你應該使用RewriteBase指令

RewriteEngine On
RewriteBase /
# Use the following rule if you want to make the page like a directory
RewriteRule ^u/(!(profile.php))$ u/$1/ [R=301,L]
# The following rule does the rewrite.
RewriteRule ^u/(!(profile.php))/$ profile.php?name=$1 [L]

這將把http://mysite.com/u/john/重寫為http://mysite.com/profile?name=john

編輯這是新的答案

RewriteEngine On
RewriteBase /
# Use the following rule if you want to make the page like a directory
RewriteRule ^u/(!(profile.php))$ u/$1/ [R=301,L]
# The following rule does the rewrite.
RewriteRule ^u/(!(profile.php))/$ profile.php?name=$1 [L]
# The following rewrite the other way round:
RewriteCond %{REQUEST_URI} ^/profile.php
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD|TRACE)\ /profile.php
RewriteCond %{QUERY_STRING} name=([^&]+)
RewriteRule ^profile.php$ u/%1? [R=301,L]

你有什么看似正確。 但是,該請求可能會再次處理。

你可以通過添加[L]標志並確保映射到確切的文件來避免這種情況。 例如:

RewriteEngine On

RewriteRule ^name/([a-zA-Z0-9]+)/?$ profile.php?name=$1 [L]

注意:我也使尾隨斜杠可選。 我明確地將RewriteEngine On

您需要在.htaccess文件中使用它:

RewriteEngine On
RewriteRule ^([^/]*)/$ /profile?name=$1 [L]

我用這個來生成它: http//www.generateit.net/mod-rewrite/

我用這種方式解決了它:

RewriteEngine On
RewriteBase /
# Use the following rule if you want to make the page like a directory
RewriteRule ^(v)$ /$1/
# The following rule does the rewrite.
RewriteRule ^(.+)/$ profile.php?id=$1
# The following rewrite the other way round:
RewriteCond %{REQUEST_URI} ^/profile.php
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD|TRACE)\ /profile.php
RewriteCond %{QUERY_STRING} id=([^&]+)
RewriteRule ^profile.php$ %1?

暫無
暫無

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

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