使用友好的 url htaccess php 重定向用户

问题描述

我需要有人来为我的产品请求重定向和编写漂亮的网址。

所以对于像这样的请求

https://www.domain.com.au/pages/product.php?product=PRODUCTNAME

重定向并重写为

https://www.domain.com.au/pages/product/PRODUCTNAME

其实我有

RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{SERVER_PORT} 80
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# Add trailing slash to url
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
RewriteRule ^(.*)$ $1/ [R=301,L]
# Remove .PHP-extension from url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.PHP -f
RewriteRule ^([^\.]+)/$ $1.PHP 
RewriteCond %{THE_REQUEST} \s/+(.+?)\.PHP[\s?] [NC]
RewriteRule ^ /%1/ [R=301,NE,L]
# End of Apache Rewrite Rules

所以它是: https://www.domain.com.au/pages/product/?product=PRODUCTNAME

对于about.PHP页面,您可能会意识到这很好。 先谢谢了!

解决方法

您可以使用以下内容来缩短您的产品网址:

将以下内容放在 /root/ 的顶部。 htaccess 文件。

RewriteEngine On
#redirect /pages/product.php?product=name to /pages/product/name
RewriteCond %{THE_REQUEST} /pages/product\.php\?product=([^\s]+) [NC]
RewriteRule ^ /pages/product/%1? [L,R=301]
 #Rewrite the new URL to the old one
#load the contents from the OLD URL
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^pages/product/([^/]+)/?$ /pages/product.php?product=$1 [L]