简体   繁体   English

URL重写–第一个值不起作用

[英]URL Rewriting – First value doesn't work

I'm trying to create friendly URLs using: 我正在尝试使用以下方法创建友好的URL:

Options +Indexes
Options +FollowSymLinks 
Options -MultiViews

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([^/]*)/([^/]*)/([^/]*)/?$ /index.php?lang=$1&page=$2&subpage=$3 [L,QSA]
</IfModule>

Everything works normally when I try: 我尝试时一切正常:

www.mydomain.com/en/my-page/ www.mydomain.com/en/my-page/

OR 要么

www.mydomain.com/en/my-page/my-subpage/ www.mydomain.com/en/my-page/my-subpage/

However, I get 404 error when I try this: 但是,尝试此操作时出现404错误:

www.mydomain.com/en/ www.mydomain.com/en/

I've been trying understand the problem. 我一直在尝试了解问题所在。 Thanks in advance. 提前致谢。

–Edit– -编辑-

Anubhava's solution works very well. Anubhava的解决方案效果很好。 In addition to that, here is another solution if you need optional parameters. 除此之外,如果需要可选参数, 这是另一种解决方案

Options +Indexes
Options +FollowSymLinks 
Options -MultiViews

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteCond %{REQUEST_FILENAME} !-f

    RewriteCond %{REQUEST_URI}  ^/([^/]+)/?([^/]*)?/?([^/]*)?/?([^/]*)?/?   [NC]
    RewriteRule .*    index.php?lang=%1&page=%2&subpage=%3&anotherkey=%4  [L]
</IfModule>

Have it like this: 像这样:

Options +Indexes
Options +FollowSymLinks 
Options -MultiViews

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} -d [OR]
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^ - [L]

    RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /index.php?lang=$1&page=$2&subpage=$3 [L,QSA]
    RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?lang=$1&page=$2 [L,QSA]
    RewriteRule ^([^/]+)/?$ /index.php?lang=$1 [L,QSA]
</IfModule>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM