0

我正在共享 HostGator 服务器上进行 LAMP 设置。我目前正在尝试创建更友好的网址。

我需要转发这样的地址:

http://usafarmtrader.com/posts/city-state/title-3869.htm

到这里:

http://usafarmtrader.com/viewpost.php?id=3869

无需更改地址栏。我当前的重写规则如下所示:

RewriteRule ^posts/(.*)/(.*)? http://usafarmtrader.com/viewpost.php?id=$2 [L,NC,P]

添加 P 标志是我可以在不更改地址的情况下加载它的唯一方法,但它似乎破坏了我的 javascript。

为了在不破坏 js 的情况下完成这项工作,我需要做什么?和 重写规则中的正则表达式应该是什么样子才能将 id(应该是最后一个破折号之后的所有内容)放入该变量中?

这是我的整个 .htaccess 文件,以防出现干扰:

#RewriteCond %{REQUEST_URI} !^/js/.*$
#RewriteCond %{REQUEST_URI} !^/img/.*$
#RewriteCond %{REQUEST_URI} !^/css/.*$
#RewriteCond %{REQUEST_URI} !^/inc/.*$
#RewriteCond %{HTTP_HOST} ^usafarmtrader.com$
#RewriteRule !^down\.php$ http://usafarmtrader.com/down.php [R=302,L]

ErrorDocument 404 /index.php

RewriteEngine On

RewriteRule ^(js|img|fonts)/ - [L]

#RewriteRule ^posts/(.*)/(.*)? http://usafarmtrader.com/viewpost.php?id=$2 [L,NC,P]
RewriteRule ^posts/[^/]+/[^.]+-([0-9]+)\.html?$ http://usafarmtrader.com/viewpost.php?id=$1 [L,NC]

RewriteCond %{SERVER_PORT} 80
RewriteRule ^mylistings\.php|login\.php|myaccount\.php|newaccount\.php|reset\.php|newpost\.php|editpost\.php$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteCond %{SERVER_PORT} 443
RewriteRule ^about\.php|contact\.php|down\.php|faq\.php|find\.php|forgot\.php|home\.php|index\.php|viewpost\.php$ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

# Google Analytics Integration - Added by cPanel.
<IfModule mod_substitute.c>
AddOutputFilterByType SUBSTITUTE text/html
Substitute "s|(<script src='/google_analytics_auto.js'></script>)?</head>|<script src='/google_analytics_auto.js'></script></head>|i"
</IfModule>
# END Google Analytics Integration

# Use PHP 5.3
AddHandler application/x-httpd-php53 .php
4

1 回答 1

1

您不需要该P标志,因为这都在同一台服务器上,您不需要代理。您的模式很接近,但也匹配 URI 的标题.htm部分。尝试类似:

RewriteRule ^posts/[^/]+/[^.]+-([0-9]+)\.html?$ /viewpost.php?id=$1 [L,NC]
于 2013-08-07T17:45:05.370 回答