簡體   English   中英

htaccess mod_rewrite:簡化URL

[英]htaccess mod_rewrite: Simplifying URL

我在以下重寫方面遇到了麻煩。 我確定已啟用mod_rewrite,但不確定我要去哪里。

我嘗試更改以下模式:

/profile/?userid=157&username=FirstL

至:

/profile/FirstL

我嘗試了許多不同的規則,但是我覺得最接近正確的兩個規則根本沒有用。 我當前的失敗如下:

RewriteEngine On
RewriteCond %{THE_REQUEST} \ /profile/+\?userid=$([^&\ ]+)&username=$([^&\ ]+)
RewriteRule ^ /%1? [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ /profile/?userid=$1&username=$2 [L,QSA]

RewriteEngine On
RewriteRule ^([^/]*)$ /profile/?userid=$1&username=$2 [L]

完整的htaccess:

    Options +FollowSymLinks -Multiviews

    RewriteEngine On
    RewriteCond %{THE_REQUEST} \ /profile/+\?userid=$([^&\ ]+)&username=$([^&\ ]+)
    RewriteRule ^ /profile/%1/%2? [L,R=301]

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^profile/([^/]+)/([^/]+)$ /profile/?userid=$1&username=$2 [L,QSA]

    RewriteBase /

    DirectorySlash Off

    RewriteRule ^admin$ /admin/index.php [L,E=LOOP:1]

    RewriteCond %{ENV:REDIRECT_LOOP} !1
    RewriteRule ^admin/index.php$ /admin [R=301,L]

    # remove .php
    RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
    RewriteRule (.*)\.php$ $1 [R=301]

    # remove index
    RewriteRule (.*)/index$ $1/ [R=301]

    # remove slash if not directory
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} /$
    RewriteRule (.*)/ $1 [R=301]

    # add .php to access file, but don't redirect
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteCond %{REQUEST_URI} !/$
    RewriteRule (.*) $1\.php [L]

    #Force non-www:
    RewriteCond %{HTTP_HOST} www.(.*)$ [NC]
    RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

您正在丟失URL的userID部分,因此當您嘗試在內部進行改寫時,那里什么也沒有:

RewriteRule ^([^/]+)$ /profile/?userid=$1&username=$2 [L,QSA]

此規則表示,第一個匹配項是“ userid”,第二個匹配項是“ username”,您只有一個匹配項,最重要的是,它甚至都不以“ profile”開頭。

您需要在URL中的某個位置包含用戶ID,否則無法提取它。

RewriteEngine On
RewriteCond %{THE_REQUEST} \ /profile/+\?userid=([^&\ ]+)&username=([^&\ ]+)
RewriteRule ^ /profile/%1/%2? [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^profile/([^/]+)/([^/]+)$ /profile/?userid=$1&username=$2 [L,QSA]

暫無
暫無

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

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