简体   繁体   English

htaccess mod_rewrite:简化URL

[英]htaccess mod_rewrite: Simplifying URL

I've been having trouble with the following rewrite. 我在以下重写方面遇到了麻烦。 I'm certain mod_rewrite is enabled but not sure where I am going wrong. 我确定已启用mod_rewrite,但不确定我要去哪里。

I'm try to change the following pattern: 我尝试更改以下模式:

/profile/?userid=157&username=FirstL

to: 至:

/profile/FirstL

I've tried many different rules, but the two I felt were the closest to being correct aren't working at all. 我尝试了许多不同的规则,但是我觉得最接近正确的两个规则根本没有用。 My current failures below: 我当前的失败如下:

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]

Full htaccess: 完整的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]

You're losing the userID part of the URL, so when you try to internally rewrite that back, there's nothing there: 您正在丢失URL的userID部分,因此当您尝试在内部进行改写时,那里什么也没有:

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

This rule says the first match is the "userid" and the second match is the "username", and you only have one match, and on top of that, it doesn't even begin with "profile". 此规则表示,第一个匹配项是“ userid”,第二个匹配项是“ username”,您只有一个匹配项,最重要的是,它甚至都不以“ profile”开头。

You'll need to include the userid somewhere in the URL, otherwise there's no way to extract it. 您需要在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