Php 如何更改htaccess中的URL
我已经在我的文件夹中创建了所有文件 现在,当我访问产品_2.php时,我得到了URLPhp 如何更改htaccess中的URL,php,.htaccess,Php,.htaccess,我已经在我的文件夹中创建了所有文件 现在,当我访问产品_2.php时,我得到了URL http://localhost/Test/product_2 现在我必须像下面这样显示URL http://localhost/Test/product/product_2 我在.htaccess中添加了RewriteRule^product/product\u 2 product\u 2.php,但它不起作用 .htaccess文件 Options +FollowSymLinks RewriteEng
http://localhost/Test/product_2
现在我必须像下面这样显示URL
http://localhost/Test/product/product_2
我在.htaccess
中添加了RewriteRule^product/product\u 2 product\u 2.php
,但它不起作用
.htaccess文件
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^product/product_2 product_2.php
您能帮我解决这个问题吗?尝试将以下行添加到您的.htaccess文件中
Redirect /Test/product/product_2 http://localhost/Test/product_2
你应该把它放在前面,稍加修改
RewriteRule ^product/product_2 product_2 [QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
不管怎样,这不是我推荐的最佳做法。。您应该将所有调用重定向到index.php,并在那里进行处理,类似于:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
然后在index.php中
$requested_url = $_GET['url'];
从这里,您可以决定要做什么,您可以包括想要的文件,甚至可以做一些更聪明的事情。我觉得将product_2放入名为productAdd
RewriteBase/Test/
的新文件夹会更容易。RewriteEngine on
@Derek,我有4个以上的文件,如果我为第二个htaccess代码创建了产品文件夹,我必须更新每个文件中的链接和脚本。你能分享一些例子吗?我应该在html锚标记中添加什么。是否正确?简单的做法是根据请求的url包含所需的文件。。高级方法是使用mvc结构来处理这些调用。您可以在此处查看:尽管您需要为itabout href.使用composer。。你可以做你决定做的任何事。。只要处理得当:)我添加了你的第一段htaccess代码和链接,但它不起作用。它显示了我应该在html锚标记中添加什么。是吗?根据描述,如果我理解正确,您希望显示URL而不是。因此,基本上您需要添加新的URL,即在href属性中。