我用 PHP 建立了一个网站,它从 csv 文件中获取汽车,通过 cron 将它们导入数据库。这些汽车显示在 showroom.php by ?id=slugname-from-database-row 的文件中,该 slug 是在导入时创建的,但是我确实缺少使重写工作的知识,因为它似乎与 news_content.php?id=slugname-from-database-row 冲突,后者显示最新新闻,并且与 showroom.php 的工作方式完全相同。
<IfModule mod_rewrite.c>
Options +FollowSymLinks
Options -Multiviews
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
#RewriteRule ^([^/]+)/?$ news_content.php?id=$1 [L]
RewriteRule ^([^/]+)/?$ showroom.php?id=$1 [L]
<IfModule>
只有页面或其他作品,如果我像 .htaccess 中那样注释掉上面的行,重写在 showroom.php 上可以正常工作:
http://127.0.0.1/showroom.php?id=604010047-honda-cr-z-3-door-15-ima-sport-hybrid-manual
被重写为:
http://127.0.0.1/604010047-honda-cr-z-3-door-15-ima-sport-hybrid-manual
如果在 .htaccess 中取消注释上述与 news_content.php 相关的行,则 showroom.php 页面将不起作用,反之亦然!
新闻页面的工作方式如上:
http://127.0.0.1/news_content.php?id=test-slug-news-item-from-database
被重写为:
http://127.0.0.1/test-slug-news-item-from-database
这让我发疯!非常感谢任何帮助!
最佳答案
试试这个
新闻
RewriteRule ^([A-Za-z0-9_\-]+).html$ news_content.php?id=$1 [L]
展厅
RewriteRule ^([A-Za-z0-9_\-]+).htm$ showroom.php?id=$1 [L]
关于php - 在动态 PHP 网站中实现 Mod Rewrite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21551436/