[英]How to simplify PHP site URL using htaccess rewrite rule?
I have this site made from the developer in PHP and now all the pages have this URL structure, 我有一个使用PHP开发人员制作的网站,现在所有页面都具有此URL结构,
websitename.com/software.php?name=product_name websitename.com/software.php?name=product_name
The only thing which changes every time on various pages on site is only the "product name" such as: 每次在网站上的各个页面上唯一更改的只是“产品名称”,例如:
websitename.com/software.php?name=product_name1 websitename.com/software.php?name=product_name1
websitename.com/software.php?name=product_name2 websitename.com/software.php?name=product_name2
websitename.com/software.php?name=product_name3 websitename.com/software.php?name=product_name3
etc 等等
I only want to have URL like this: 我只想要这样的网址:
websitename.com/product_name1 websitename.com/product_name1
websitename.com/product_name2 websitename.com/product_name2
websitename.com/product_name3 and so on.. websitename.com/product_name3等。
can you please tell me what code should I be adding on my htaccess in order to get such URL structure. 您能告诉我我应该在htaccess上添加什么代码才能获得这样的URL结构。
For websitename.com/any to websitename.com/software.php?name=any use this rules in top of yours .htaccess file 对于websitename.com/any到websitename.com/software.php?name=any,请在您的.htaccess文件顶部使用此规则
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /software.php?name=$1 [L]
</IfModule>
But more usefull URL structure (software only for products) would be websitename.com/software/any-product-name to websitename.com/software.php?name=any-product-name 但更有用的URL结构(仅用于产品的软件)将是websitename.com/software/any-product-name到websitename.com/software.php?name=any-product-name
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^software\/(.*)$ /software.php?name=$1 [L]
</IfModule>
For SEO friendly URLs use "-" instead of "_" in product names. 对于SEO友好的URL,在产品名称中使用“-”代替“ _”。
This should point you into the right direction: 这应该为您指明正确的方向:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.*)$ /software.php?name=$1 [END]
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. 如果您使用上述规则收到内部服务器错误(http状态500),则很可能是您运行了非常旧版本的apache http服务器。 You will see a definite hint to an unsupported
[END]
flag in your http servers error log file in that case. 在这种情况下,您将在http服务器错误日志文件中看到对不支持的
[END]
标志的明确提示。 You can either try to upgrade or use the older [L]
flag, it probably will work the same in this situation, though that depends a bit on your setup. 您可以尝试升级或使用旧的
[L]
标志,在这种情况下它可能会起作用,尽管这在一定程度上取决于您的设置。
This rule will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). 此规则将在http服务器主机配置或动态配置文件(“ .htaccess”文件)中同样起作用。 Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host.
显然,重写模块需要加载到http服务器内部并在http主机中启用。 In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's
DOCUMENT_ROOT
folder. 如果使用动态配置文件,则需要注意在主机配置中完全启用了它的解释,并且该解释位于主机的
DOCUMENT_ROOT
文件夹中。
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). 还有一个一般性说明:您应该始终喜欢将此类规则放在http服务器主机配置中,而不要使用动态配置文件(“ .htaccess”)。 Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server.
这些动态配置文件增加了复杂性,通常是导致意外行为,难以调试的原因,并且确实降低了http服务器的速度。 They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).
仅当您无法访问真正的http服务器主机配置(阅读:真正便宜的服务提供商)或坚持编写自己的规则的应用程序(这显然是安全的噩梦)时,才提供它们作为最后的选择。
UPDATE: In the comment below you ask in addition to redirect a request to the original, not rewritten URL. 更新:在下面的注释中,您除了要求将请求重定向到原始URL以外,还要求其他URL。 Here is an extended version doing that:
这是这样做的扩展版本:
RewriteEngine On
RewriteCond %{QUERY_STRING} (?:^|&)name=([^&]+)(?:&|$)
RewriteRule ^/?software\.php$ /%1 [R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.*)$ /software.php?name=$1 [END]
It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. 一个好主意是从302临时重定向开始,然后在确定一切都已正确设置之后,才将其更改为301永久重定向。 That prevents caching issues while trying things out...
这样可以防止在尝试时缓存问题...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.