【问题标题】:htaccess rule giving 500 internal server errorhtaccess 规则给出 500 内部服务器错误
【发布时间】:2021-09-28 05:32:19
【问题描述】:

我在尝试重写 URL 时遇到错误。

从这里:

www.example.com/products/product-details.php?alias=abc-def-123

到这里:

www.example.com/products/abc-def-123

这是我的 .htaccess 文件

#Turn Rewrite Engine On
Options +FollowSymLinks -MultiViews
Options -Indexes

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

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

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [NC,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^products/([a-zA-Z0-9-]+)$ products/product-details.php?alias=$1 [NC,L]

</IfModule>

我正在点击这个网址:

www.example.com/products/abc-def-123

更新:

【问题讨论】:

  • 欢迎来到 SO,感谢您分享您的努力。您能否确认您是否在浏览器中点击www.example.com/products/product-details.php?alias=abc-def-123 链接?
  • 是的,没有出现任何错误

标签: .htaccess mod-rewrite url-rewriting friendly-url


【解决方案1】:

对于您展示的示例,请尝试以下规则。请确保将您的 htaccess 与您的产品文件夹(不在其中)一起保存在根文件夹中。

<IfModule mod_rewrite.c>
Options +FollowSymLinks -MultiViews -Indexes
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)/?$ https://example.com/$1 [L,NE,R=301]

RewriteCond %{THE_REQUEST} \s/products/.*\?alias=(\S+)\s
RewriteRule ^ /products/%1? [R=301,L]

RewriteRule ^products/(.*)/?$ products/product-details.php?alias=$1 [NC,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f [NC]
RewriteRule ^(.*)/?$ $1.html [NC,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f [NC]
RewriteRule ^(.*)/?$ $1.php [NC,L]

</IfModule>

【讨论】:

  • 没有任何区别。仍然收到 500 内部服务器错误
  • @PRASHANTVERMA,请确认您的 htaccess 文件和 php 文件的路径?
  • 我已确认 .htaccess 文件位于根目录中,即示例文件夹中。此外,其他重写规则正在工作,但这个不工作。请解决我的问题
  • @PRASHANTVERMA,请提及 2 件事,1-您点击示例一的网址是什么? 2-除了我发布的规则之外,您还有其他规则吗?请确认这些,我会在早上看看,因为这里是深夜。
  • 我已通过向问题添加更多详细信息来满足您的要求。
【解决方案2】:

是的!!!!!!! 我自己解决了我的问题,也感谢@RavinderSingh13 的努力。

这是整个 .htaccess 文件

<IfModule mod_rewrite.c>
Options +FollowSymLinks -MultiViews -Indexes
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)/?$ https://example.com/$1 [L,NE,R=301]

RewriteRule ^products/([^/\.]+)?$ products/product-details.php?alias=$1 [NC,L]  

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f [NC]
RewriteRule ^(.*)/?$ $1.html [NC,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f [NC]
RewriteRule ^(.*)/?$ $1.php [NC,L]

</IfModule>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-22
    相关资源
    最近更新 更多