【发布时间】:2016-12-22 00:41:07
【问题描述】:
所以我使用以下 htaccess 代码将 URL 重定向到干净的 URL,但是,现在我需要做的是让原始 URL 重定向到新的干净 URL。
示例
原网址:
example.com/search/store_info.php?store=113&dentist=Dr.%20John%20Doe
干净的网址:
example.com/search/113/dr-john-doe
我需要的是“ORIGINAL URL”来重定向到“CLEAN URL”。我需要这样做的原因是这两个 URL 都出现在 Google 搜索中。
我只希望显示干净的 URL,并且只要使用原始 URL,它就会自动重定向到干净的 URL。它目前不这样做。
这是我的 htaccess 文件。
ErrorDocument 404 default
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /search/
RewriteCond %{QUERY_STRING} .
RewriteCond %{THE_REQUEST} /store_info\.php\?store=([a-z0-9]+)&dentist=([^\s&]+) [NC]
RewriteRule ^ %1/%2/? [L,NE,R=301]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_URI} \.(?:jpe?g|gif|bmp|png|ico|tiff|css|js)$ [NC]
RewriteRule ^ - [L]
RewriteRule ^([a-z0-9]+)/([^\s.]*)[.\s]+(.*)$ $1/$2-$3 [NC,DPI,E=DONE:1]
RewriteCond %{ENV:DONE} =1
RewriteRule ^([0-9a-z]+)/([^\s.]+)$ $1/$2 [R=301,NE,L]
RewriteRule ^([a-z0-9]+)/([a-z0-9-]+)/?$ store_info.php?store=$1&dentist=$2 [QSA,L,NC]
</IfModule>
我已经阅读了 RedirectMatch,但是我不知道在哪里将它实现到我的 htaccess 文件中。
【问题讨论】:
-
@anubhava 它不会重定向.. 因此,如果我转到
example.com/search/store_info.php?store=113而 URL 中没有“&dentist=Dr.%20John%20Doe”,它会转到原始 URL 并且不会转换到干净的 URL 版本。我希望将其更改为example.com/search/store_info.php?store=113也转到干净 URL 的位置。我试过Redirect 301 /search/store_info.php?store=118 http://www.example.com/search/118/John-Doe-Dental-Group/ -
我可以补充一点,
&dentist=Dr.%20John%20Doe通过上一页的按钮添加到 URL 字符串。所以example.com/search/store_info.php?store=113是从 URL 中获取牙医姓名的原始 URL,然后使用 htaccess 文件创建干净的 URL。 -
@anubhava 所以没有办法像this 那样做吗?我知道 web.config 文件中有一种方法。但我无法在 htaccess 中弄清楚。
-
不能像this这样的htaccess文件功能吗?我只是想将原始 URL 插入到 htaccess 文件中,然后将其作为 301 重定向到 NEW/clean URL
-
如何将“静态值”添加到我现有的 htaccess 文件中?是否必须像为 Web 配置文件一样为每个 URL 创建“静态值”?
标签: apache .htaccess mod-rewrite url-redirection friendly-url