如何解决Htaccess中的分页URL结构
我的网址结构为 mysite.com/category.PHP?c=abc&page=4
我需要将 URL 结构设为 mysite.com/category/abc/page/4
RewriteEngine On
<IfModule mod_rewrite.c>
https redirect Rule Here
</IfModule>
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.PHP -f
RewriteRule ^(.*)$ $1.PHP
RewriteRule ^([-\w]+)$ blog-post.PHP?slug=$1 [NC,L]
RewriteRule ^amp/([-\w]+)$ amp/amp.PHP?slug=$1 [NC,L]
RewriteRule ^mirror/([-\w]+)$ mirror/post.PHP?slug=$1 [NC,L]
RewriteRule ^category/([a-zA-Z-]+) category.PHP?c=$1 [NC,L]
RewriteRule ^category/([a-zA-Z-/]+)/page/([0-9]+) category.PHP?c=$1&page=$2 [NC,L]
RewriteRule ^mirror/category/([a-zA-Z-]+) mirror/category.PHP?c=$1 [NC,L]
RewriteRule ^Feed/([a-zA-Z-]+)$ Feed/Feed.PHP?f=$1 [NC,L]
RewriteRule ^author/([a-zA-Z0-9-]+) author.PHP?name=$1 [NC,L]
RewriteRule ^tag/([a-zA-Z0-9-]+) tag.PHP?t=$1 [NC,L]
RewriteRule ^mirror/tag/([a-zA-Z0-9-]+) mirror/tag.PHP?t=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [R=301,L]
RewriteRule ^sitemap\.xml/?$ sitemap.PHP
RewriteRule ^news-sitemap\.xml/?$ news-sitemap.PHP
RewriteRule ^image-sitemap\.xml/?$ image-sitemap.PHP
RewriteRule ^author-sitemap\.xml/?$ author-sitemap.PHP
ErrorDocument 404 /404.PHP
ErrorDocument 500 Error
Options -Indexes
我面临的问题是当我尝试使用 $_get[]
变量获取页码时,我无法获取它。休息一下,我可以通过 $_get[]
解决方法
你有很多规则。让我尝试让您继续前进:
ErrorDocument 404 /404.php
Options -Indexes -MultiViews
RewriteEngine On
<IfModule mod_rewrite.c>
https redirect Rule Here
</IfModule>
## Unless directory,remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.+)/+$
RewriteRule ^ %1 [R=301,NE,L]
## ignore all URIs for actual files and directories
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^amp/([-\w]+)$ amp/amp.php?slug=$1 [NC,L,QSA]
RewriteRule ^mirror/([-\w]+)$ mirror/post.php?slug=$1 [NC,QSA]
RewriteRule ^category/([\w-]+)$ category.php?c=$1 [NC,QSA]
RewriteRule ^category/([\w-]+)/page/(\d+)$ category.php?c=$1&page=$2 [NC,QSA]
RewriteRule ^mirror/category/([\w-]+)$ mirror/category.php?c=$1 [NC,QSA]
RewriteRule ^feed/([\w-]+)$ feed/feed.php?f=$1 [NC,QSA]
RewriteRule ^author/([\w-]+)$ author.php?name=$1 [NC,QSA]
RewriteRule ^tag/([\w-]+)$ tag.php?t=$1 [NC,QSA]
RewriteRule ^mirror/tag/([\w-]+)$ mirror/tag.php?t=$1 [NC,QSA]
RewriteRule ^sitemap\.xml$ sitemap.php [NC,L]
RewriteRule ^((?:news|image|author)-sitemap)\.xml$ $1.php [NC,L]
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteRule ^([-\w]+)$ blog-post.php?slug=$1 [QSA,L]
确保在完全清除浏览器缓存后对其进行测试,或者从命令行 curl
进行测试。
使用您显示的示例,请尝试以下操作。在测试您的网址之前,请务必清除浏览器缓存。
您需要注意两点。 1st- 您没有使用 $
锚点来确保您的第一个规则仅匹配 URI,例如 http://locahost:80/category/testtest
所以发生的事情您的规则与两个 URI 都匹配。第二 - 您是否需要添加条件以确保这些规则仅适用于不存在的东西(在理想情况下)。
RewriteEngine On
Options -Indexes
<IfModule mod_rewrite.c>
##https redirect Rule Here
</IfModule>
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.*)$ $1.php [L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^([-\w]+)$ blog-post.php?slug=$1 [NC,L]
RewriteRule ^amp/([-\w]+)$ amp/amp.php?slug=$1 [NC,L]
RewriteRule ^mirror/([-\w]+)$ mirror/post.php?slug=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^category/([a-zA-Z-]+)/?$ category.php?c=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^category/([a-zA-Z-/]+)/page/([0-9]+)/?$ category.php?c=$1&page=$2 [NC,L]
RewriteRule ^mirror/category/([a-zA-Z-]+) mirror/category.php?c=$1 [NC,L]
RewriteRule ^feed/([a-zA-Z-]+)$ feed/feed.php?f=$1 [NC,L]
RewriteRule ^author/([a-zA-Z0-9-]+)$ author.php?name=$1 [NC,L]
RewriteRule ^tag/([a-zA-Z0-9-]+)$ tag.php?t=$1 [NC,L]
RewriteRule ^mirror/tag/([a-zA-Z0-9-]+)$ mirror/tag.php?t=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [R=301,L]
RewriteRule ^sitemap\.xml/?$ sitemap.php [NC,L]
RewriteRule ^news-sitemap\.xml/?$ news-sitemap.php [NC,L]
RewriteRule ^image-sitemap\.xml/?$ image-sitemap.php [NC,L]
RewriteRule ^author-sitemap\.xml/?$ author-sitemap.php [NC,L]
ErrorDocument 404 /404.php
ErrorDocument 500 Error
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。