.htaccess 如何使用htaccess通过将ID替换为产品名称来更改URL

slwdgvem  于 2022-11-16  发布在  其他
关注(0)|答案(1)|浏览(103)

我的ecom站点中的每个产品都是使用URL中传递的产品名称打开的,这将使URL看起来像这样:

www.mysite.com/Products/Product-Name

但它试图在文件夹**“Products”中查找名为“Product-Name”**的页面。实际上,我没有任何这些文件夹。
相反,我如何在htaccess中编写条件以不查找页面,这样我就可以获取URL的最后一部分(即Product name并从DB中获取数据)和URL的倒数第二部分,以标识用户正在搜索的内容。

到目前为止,我的.htaccess文件为:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/cpanel-dcv/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/(?:\ Ballot169)?
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([^\.]+)$ $1.html [NC,L]

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/cpanel-dcv/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/(?:\ Ballot169)?
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

先谢谢你。
(In一瞥):
我的URL:**www.mysite.com/Products/Product-Name但是它应该打开页面:****
(By保持URL与“www.mysite.com/Products/Product-Name”相同,以便我可以使用JS来获取要使用的URL部分)

ruyhziif

ruyhziif1#

请确保在测试你的网址之前清除你的浏览器缓存。除了在你已经尝试过的.htaccess文件中添加新的规则之外,我还改变了一些规则的顺序,例如:非http --〉http规则应该位于.htaccess规则文件的顶部。
还要确保.htaccess规则文件和products.php仅位于同一文件夹/目录中。

RewriteEngine ON

RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/cpanel-dcv/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/(?:\ Ballot169)?
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

##New rules added for internal rewrite from here.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^Products/Product-Name/?$ products.php [QSA,NC,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/cpanel-dcv/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/(?:\ Ballot169)?
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([^\.]+)$ $1.html [NC,L]

相关问题