簡體   English   中英

.htaccess重寫多個網址

[英].htaccess rewrite multiple urls

我需要有關.htaccess文件重寫的幫助。 因此,這就是我現在可以完成的工作,但是當我嘗試添加新的RewriteRule時,什么也沒有發生。 我要重寫的網址是index.php?page = $ 1

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ profile.php?username=$1

所以當我這樣做時:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ profile.php?username=$1
RewriteRule ^(.*)$ index.php?page=$1

當我這樣做時,頁面沒有任何CSS:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ profile.php?username=$1
RewriteRule ^(.*_)$ index.php?page=$1

該頁面具有css,但我仍然可以獲得index.php?page = pagetitle。 但是個人資料頁面確實提供了我/ 用戶名

RewriteRule ^(.*)$ profile.php?username=$1
RewriteRule ^(.*)$ index.php?page=$1

您正在要求服務器將每個URL重定向到兩個不同的頁面,它無法正常工作,服務器無法僅猜測要加載的頁面。

您需要的是/ profile / username規則或/ page / pagetitle規則。 IE瀏覽器類似:

RewriteRule ^profile/(.*)$ profile.php?username=$1 [QSA]
RewriteRule ^(.*)$ index.php?page=$1 [L]

您的重寫規則基於正則表達式,因此需要盡可能具體,以便服務器可以准確確定要使用的url-例如,如何確定http://example.com/something是頁面還是個人資料? 在URL上使用前綴“用戶”,“配置文件”等意味着可以將http://example.com/profile/something作為用戶名進行重定向,並為其他所有內容使用默認重定向。 為此,您需要首先匹配更具體的模式(用戶),並利用[L]指令指示不應處理以下規則。 我通常對URL使用否定字符類來匹配正斜杠[^/]*

# Enable mod_rewrite
RewriteEngine On
# Set the base directory
RewriteBase /
# Don't process if this is an actual file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Does this url start with /profile and then followed with additional characters?
RewriteRule ^profile/([^/]*)$ profile.php?username=$1 [NC,L]
# Assume everything else is a page
RewriteRule ^(.*)$ index.php?page=$1 [NC,L]

http://htaccess.madewithlove.be/上進行測試(請注意,不支持%{REQUEST_FILENAME}%{REQUEST_FILENAME}進行測試)。

輪廓

input url
http://www.example.com/profile/something

output url
http://www.example.com/profile.php

debugging info
1 RewriteRule ^profile/([^/]*)$ profile.php?username=$1 [NC,QSA,L]  
    This rule was met, the new url is http://www.example.com/profile.php
    The tests are stopped because the L in your RewriteRule options
2 RewriteRule ^(.*)$ index.php?page=$1 [NC,L]

input url
http://www.example.com/something

output url
http://www.example.com/index.php

debugging info
1 RewriteRule ^profile/([^/]*)$ profile.php?username=$1 [NC,L]  
2 RewriteRule ^(.*)$ index.php?page=$1 [NC,L]
    This rule was met, the new url is http://www.example.com/index.php
    The tests are stopped because the L in your RewriteRule options

暫無
暫無

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

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