简体   繁体   English

使用php重写URL无法正常工作

[英]URL rewriting using php not working

I have my url as http://public_html.com/sub-products.php?catid=42 我的网址为http://public_html.com/sub-products.php?catid=42

I want it to look like http://public_html.com/sub-products/catid/42 我希望它看起来像http://public_html.com/sub-products/catid/42

I have made a .htaccess file too. 我也做了一个.htaccess文件。 but still there is no change in my url 但我的网址仍然没有变化

Options +FollowSymLinks

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_FILENAME}\.php -f

RewriteRule ^(.*)$ $1.php

RewriteRule   ^sub-products/catid/([0-9]+)/?$   sub-products.php?catid=$1 [L]

Do i need to change anything in my php file also? 我是否还需要更改php文件中的任何内容?

what code is required for php file to use .htaccess file? php文件使用.htaccess文件需要什么代码?

Maybe there is a purpose to your line 也许您的路线有目的

RewriteRule ^(.*)$ $1.php

But i can't see it for the need you have, so i guess a 但是我看不到它的需要,所以我想

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^sub-products/catid/([0-9]+)/?$ /sub-products.php?catid=$1 [L]

should work. 应该管用。

Edit : 编辑:

This should work if you are trying to access the http://public_html.com/sub-products/catid/42 url 如果您尝试访问http://public_html.com/sub-products/catid/42网址,则此方法应该有效

(or http://public_html.com/sub-products/catid/42/ btw) (或http://public_html.com/sub-products/catid/42/ btw)

If what you want is going from 如果你想要什么

http://public_html.com/sub-products.php?catid=42 http://public_html.com/sub-products.php?catid=42

to

http://public_html.com/sub-products/catid/42/ http://public_html.com/sub-products/catid/42/

You might need something like this : 您可能需要这样的东西:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{QUERY_STRING} ^catid=([0-9]+)$
RewriteRule ^sub-products.php$ /sub-products/catid/%1/? [R=301,L]

And then, you can use what i said before. 然后,您可以使用我之前所说的内容。

Your rule in .htaccess rewrite only incoming seo-pretty URls to URL with php script filename. 您在.htaccess中的规则仅将传入的seo漂亮URls重写为具有php脚本文件名的URL。 But if you need construct url as /sub-products/catid/42 you have to change you PHP code, that render HTML output. 但是,如果您需要将url构造为/ sub-products / catid / 42 ,则必须更改PHP代码,以呈现HTML输出。

for example 例如

while ( $products as $product ) {
  echo '<a href="/sub-products/catid/"'.$product['id'].'>'.$product['name'].'</a>;
}

I am not sure if you switched your first statment around, but it looks in your .htaccess file you want to rewrite the other way around. 我不确定您是否切换了第一个语句,但是它在您的.htaccess文件中以其他方式重写。 Maybe this would work. 也许这会工作。

Options +FollowSymLinks
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} catid=([0-9]+)$
RewriteRule ^/?sub-products.php /sub-products/catid/%1 [R,L]

If you want to have a more generic rule, and not just for catid this RewriteRule could work: 如果您想要一个更通用的规则,而不仅仅是catid话,那么RewriteRule可以工作:

RewriteCond %{QUERY_STRING} ([a-zA-Z]+)=([0-9]+)$
RewriteRule ^/?sub-products.php /sub-products/%1/%2 [R,L]

Hope it helps you forward. 希望它能帮助您前进。

EDIT: Made some changes after testing it on my system. 编辑:在我的系统上对其进行测试后进行了一些更改。

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

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