简体   繁体   English

.htaccess重写规则不包括index.php

[英].htaccess rewrite rule to not include index.php

I have a .htaccess rule that GETs the first directory part of the url and uses that to look up the product from the database as follows www.website.com/product-1. 我有一个.htaccess规则,该规则获取url的第一个目录部分,并使用该规则从数据库中查找产品,如下www.website.com/product-1。 I use the following rule, and it works fine: 我使用以下规则,效果很好:

RewriteRule ^([-_a-z0-9]+)?$ /product.php?product_url=$1 [NC,L]

The problem is, when I visit the home page www.website.com/ it tries to look for a blank product. 问题是,当我访问主页www.website.com/时,它试图查找空白产品。 If I want to visit the home page, I have to use www.website.com/index.php. 如果要访问主页,则必须使用www.website.com/index.php。

How can I keep the url structure I have above, but make www.website.com/ show the home page index.php? 如何保持上面的url结构,但使www.website.com/显示主页index.php?

Main issue is with your regex that also matches an empty string. 主要问题是您的正则表达式也匹配一个空字符串。

You should be using this rule instead: 您应该改用以下规则:

RewriteRule ^([-_a-z0-9]+)/?$ product.php?product_url=$1 [NC,L,QSA]

This regex makes trailing slash optional in request URI but doesn't match empty string (landing page) 此正则表达式使请求URI中的斜杠为可选,但与空字符串不匹配(登录页面)

Additionally consider using these 2 conditions to skip all files and directories from this rewrite: 另外,考虑使用以下两个条件来跳过此重写中的所有文件和目录:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([-_a-z0-9]+)/?$ product.php?product_url=$1 [NC,L,QSA]

This should skip index.php 这应该跳过index.php

RewriteRule ^index\.php$ - [L]
RewriteRule ^([-_a-z0-9]+)?$   /product.php?product_url=$1    [NC,L]

You can try this : ALSO check and modify this question I once asked to see if you can tweak it up Perform If statements for a specific file name in .htaccess 您可以尝试以下操作:还请检查并修改这个问题,我曾经问过您是否可以对其进行调整,以查看.htaccess中特定文件名的Perform If语句。

RewriteRule ^index\.php$ - [L] //This would make it skip index.php and look at product.php
RewriteRule ^([-_a-z0-9]+)?$   /product.php?product_url=$1    [NC,L]

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

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