简体   繁体   English

用HTACCESS重写动态URL

[英]Re-writing Dynamic URL with HTACCESS

I've been reading a few tutorials online and searched a couple of other questions here but I cant seem to grasp the re-writing of dynamic URLS using the HTACCESS file. 我已经在线阅读了一些教程,并在这里搜索了其他几个问题,但是我似乎无法掌握使用HTACCESS文件对动态URL进行重写的方法。

I have URLs like: 我有以下网址:

products.php?cat=2
products.php?cat=3
products.php?cat=4

I would like them to just say products/2 for example 我希望他们只说产品/ 2

equipment.php?cat=2&subCat=1
equipment.php?cat=2&subCat=2
equipment.php?cat=2&subCat=3

I would like these to say equipment/2/1 for example 我想要这些例如说equipment / 2/1

product.php?id=3010-Z89CH24

I would like these to say product/3010-Z89CH24 for example 我想以这些为例说product / 3010-Z89CH24

but so far just trying to change the products.php pages I have this: 但是到目前为止,只是尝试更改products.php页面,我这样做:

    RewriteEngine On
RewriteCond %{HTTP_HOST} ^*******\.com [NC]
RewriteRule (.*) http://www.*******.com/$1 [L,R=301]

Options +FollowSymLinks

RewriteRule ^products/'^([1-9][0-9]{0,2})'$ http://www.*******.com/products.php?cat=/$2 [L] 
# REMOVE PHP EXTENSIONS
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L,QSA]

the thing is I dont think the end part "/$2" is correct and im unsure of the rest of it too :/ 问题是我不认为结尾部分“ / $ 2”是正确的,也不确定其余部分:/

Either way its not working for me and I am stuck and struggling to understand it :( 不管哪种方式,它对我都不起作用,我被困在并且难以理解它:(

can someone help me please? 有人能帮助我吗?

Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your DOCUMENT_ROOT/.htaccess file: 通过httpd.conf启用mod_rewrite.htaccess ,然后将此代码放入DOCUMENT_ROOT/.htaccess文件中:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

## If the request is for a valid directory
RewriteCond %{REQUEST_FILENAME} -d [OR]
## If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f [OR]
## If the request is for a valid link
RewriteCond %{REQUEST_FILENAME} -l
## don't do anything
RewriteRule ^ - [L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+([^.]+)\.php\?cat=([^\s&]+)&subcat=([^\s&]+)\s [NC]
RewriteRule ^ /%1/%2/%3? [R=301,L]

RewriteRule ^([^/.]+)/([^/]+)/([^/]+)/?$ /$1.php?cat=$2&subcat=$3 [L,QSA]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+([^.]+)\.php\?cat=([^\s&]+)\s [NC]
RewriteRule ^ /%1/%2? [R=301,L]

RewriteRule ^([^/.]+)/([^/]+)/?$ /$1.php?cat=$2 [L,QSA]

Your problem is that your regexp is wrong - I'd recommend checking out some kind of basic regular-expression tutorial, paying attention to mentions of capture groups. 您的问题是您的正则表达式是错误的-我建议您查看某种基本的正则表达式教程,并注意捕获组的提及。

Without checking - and assuming that your digit-based-restrictions are correct - I'd guess you want a rule something like this: 如果不进行检查-并假设您的基于数字的限制是正确的-我猜您想要这样的规则:

RewriteRule  ^/products/([1-9][0-9]{0,2})$  /products.php?cat=$1 [L]

If you need to preserve existing query-strings you'll probably want the QSA flag too. 如果您需要保留现有的查询字符串,则可能还需要QSA标志。

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

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