简体   繁体   English

在htaccess中为多个页面重写URL

[英]Rewrite URLs in htaccess for multiple pages

I need to redirect urls from two different pages to readable URLS. 我需要将网址从两个不同的页面重定向到可读的URL。 my .htaccess looks like this 我的.htaccess像这样

Options +FollowSymLinks -MultiViews -Indexes
RewriteEngine On
RewriteRule ^([a-zA-Z0-9-/]+)$ blog-detail.php?url=$1
RewriteRule ^([a-zA-Z0-9-/]+)/$ blog-detail.php?url=$1
RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)$ page.php?pid=$1
RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/$ page.php?pid=$1 

The first urls using the blog-detail.php work fine. 使用blog-detail.php的第一个网址可以正常工作。 My URLS can be like mysite.com/latest-news-story 我的网址可能像mysite.com/latest-news-story

However, the second with page.php does not work unless I remove the blog-detail rewrite rules. 但是,除非删除博客详细信息重写规则,否则第二个带有page.php的文件将不起作用。 How can I have both? 我怎么都可以?

You can actually combine your rules that check for terminating / into one. 您实际上可以将检查终止/规则合并为一个。

So, you actually just need the following two rules. 因此,您实际上只需要以下两个规则。

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

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ blog-detail.php?url=$1 [QSA,L]

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

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