简体   繁体   English

.htaccess中的多个重写规则以重定向到所需的页面

[英]Multiple Rewrite Rules in .htaccess To Redirect at desired pages

I am trying setup a site where i am creating permalinks just like wordpress but specific to the site. 我正在尝试建立一个网站,我在其中创建永久链接,就像wordpress一样,但特定于该网站。 So, when a user clicks on article it should should like 因此,当用户点击文章时,

http://www.sitename.com/url-slug

Also when user clicks on directory it should redirect something like this 另外,当用户单击目录时,它应该重定向这样的内容

http://www.sitename.com/category/category-slug

Similarly, when clicking on sets, users, pages it should redirect respectively like 同样,当点击集,用户,页面时,其应分别重定向,例如

http://www.sitename.com/sets/set-slug

http://www.sitename.com/user/user-full-name
http://www.sitename.com/page/page-slug

Following is the code i am using in .htaccess which i am pretty sure is incorrect 以下是我在.htaccess中使用的代码,我敢肯定这是不正确的

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule (.*)$ ./single.php?id=$1 
RewriteRule ^category/(.*)/ category.php?id=$1 [L]


Options +FollowSymLinks
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ http://www.sitename.com/$1 [R=301,L]

Could someone guide me where i am going wrong, i need these pages to call their respective pages ie single.php, category.php, sets.php, users.php and page.php respectively to fetch and display data. 有人可以指导我哪里出问题了,我需要这些页面来调用它们各自的页面,即single.php,category.php,sets.php,users.php和page.php来分别获取和显示数据。 I am a newbie in .htaccess so, any help is appreciated. 我是.htaccess的新手,因此,感谢您的帮助。

I am assuming that you have no other rules in your .htaccess located int the root directory of you website 我假设.htaccess中没有其他规则,位于您网站的根目录中

RewriteEngine on
RewriteBase /

#if for an existing directory or file
RewriteCond %{SCRIPT_FILENAME} -d
RewriteCond %{SCRIPT_FILENAME} -f
#then do nothing
RewriteRule . - [L]

#otherwise
#if it is for a slug (assuming alphanumeric and dashes), send to single
RewriteRule ^([-a-zA-Z0-9]+)/?$ single.php?id=$1 [L]

#if a category 
RewriteRule ^category/([-a-zA-Z0-9]+)/?$ category.php?id=$1 [L,NC]

#if a sets
RewriteRule ^sets/([-a-zA-Z0-9]+)/?$ sets.php?id=$1 [L,NC]

#if a sets
RewriteRule ^user/([-a-zA-Z0-9]+)/?$ users.php?id=$1 [L,NC]

#if a page
RewriteRule ^page/([-a-zA-Z0-9]+)/?$ page.php?id=$1 [L,NC]

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

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