大网数据广告活动
网站运营与故障问答

Multiple rules to rewrite the url

· 52CE 我爱测

Why both rules don't work together and how can i fix it? Only the first one works... I think that there's a conflict between the two rules, but i did not found a solution yet ?p= is the querystring i use to access the files that …

问题原文

原帖链接 · mario · CC BY-SA 4.0

Why both rules don't work together and how can i fix it? Only the first one works... I think that there's a conflict between the two rules, but i did not found a solution yet

?p= is the querystring i use to access the files that are into the main directory of my website, and ?page= is for a sub-directory.

My current URLs are:

https://example.com/?p=home
https://example.com/?page=article
https://example.com/?page=article&tab=2

And i want it to become:

https://example.com/home
https://example.com/article
https://example.com/article/2

.htaccess:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ index.php?p=$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ index.php?page=$1 [L]

已采纳解答

原帖链接 · MrWhite · CC BY-SA 4.0

Yes, you have a conflict.

  • Both your .htaccess rules have exactly the same conditions, so the first one is always going to win. The second is never going to be executed.
  • Your URL structure is ambiguous.

You need to take a step back, before trying to write any code, how would you resolve the following URLs:

  • https://example.com/home
  • https://example.com/article
  • https://example.com/foo

How do you know that home should resolve to ?p=home and article should resolve to ?page=article? What about foo? You can only "see" what's in the URL.

You need to either:

  • Have some discernible difference between the URLs, so you know how to rewrite it. For example: example.com/p/home vs example.com/pg/home
  • If the number of URLs rewritten to ?p= (or ?page=) is limited then you can literally list the URL patterns in your directive. And everything else goes to ?page= (or ?p=). For example:

    RewriteRule ^(home|about)$ index.php?p=$1 [L]
    :
    RewriteRule ^([^/]+)$ index.php?page=$1 [L]
    
  • Rewrite everything to a single URL parameter (eg. ?page=) and decide in your PHP how the page should be served. This would seem to be the more logical solution since you are rewriting to index.php in both scenarios anyway and you don't want to keep having to edit .htaccess. This could also handle the additional tab "parameter".

The additional "tab" parameter

If all URLs that have a second path segment should be rewritten to ?page= then that could be a "discernable" difference. However, that currently fails when the second path segment is omitted, as in /article. How should this be handled?

the files to ?page= are into a sub-directory, and the URL could be this: https://example.com/news-folder/article

Your question made no reference to a subdirectory. This would be enough to differentiate the "type" of URLs and to be able to rewrite the URLs differently.

For example:

RewriteEngine On

# Stop processing if the request looks like it maps to a physical resource
RewriteCond %{REQUEST_URI} \.(?:gif|jpg|png|css|js)$ [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

# Single path segment rewrite to ?p=
RewriteRule ^([^/]+)$ index.php?p=$1 [L]

# Requests for files in the subdirectory rewrite to ?page=
RewriteRule ^news-folder/([^/]+)$ index.php?page=$1 [L]

# Requests for files in the subdirectory with additional numeric "tab"
RewriteRule ^news-folder/([^/]+)/(\d+)$ index.php?page=$1&tab=$2 [L]

The last two rules could be combined if you are OK having an empty tab parameter. eg. example.com/news-folder/article would be rewritten example.com/index.php?page=article&tab=. As it stands, the directives are separate, so the tab parameter is not present at all when /2 (for example) is not appended.

UPDATE: The filesystem checks (for static resources and to prevent a rewrite loop) could be omitted entirely if you are more restrictive with the regex you use to target your article pages. For example:

RewriteEngine On

# Single path segment rewrite to ?p=
RewriteRule ^([^/.]+)$ index.php?p=$1 [L]

# Requests for files in the subdirectory rewrite to ?page=
RewriteRule ^news-folder/([^/.]+)$ index.php?page=$1 [L]

# Requests for files in the subdirectory with additional numeric "tab"
RewriteRule ^news-folder/([^/.]+)/(\d+)$ index.php?page=$1&tab=$2 [L]

This just means that your URLs to your pages cannot contain . (dot). You could be more restrictive and only allow word characters (plus hyphen) and change [^/.]+ to [\w-]+. This then allows article names that only consist of the characters: 0-9, a-z, A-Z, _ and -.

You can make the subdirectory /news-folder entirely generic and match any subdirectory. For example:

RewriteRule ^\w+/(\w+)$ index.php?page=$1 [L]

Now, any request for /<something>/article would be rewritten to index.php?page=article.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2018-12-11)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。