如何使用.htaccess转换/重新映射URL

时间:2015-11-25 17:20:29

标签: .htaccess redirect

我想在我的网站上进行重定向。

旧网址如下:

http://www.example.com/Prem_Ratan_Dhan_Payo/Prem_Leela.html

现在我们必须将其重定向到

http://example.com/songs/prem-leela/

总的来说,我必须

  1. 检查网址是否以.html结尾,然后我们会将其重定向,以便让它通过。
  2. 从结尾删除.html。
  3. 以小写字母表示所有字母。
  4. 将下划线改为连字符。
  5. 将第二段改为'歌曲'。
  6. 请帮帮我。

1 个答案:

答案 0 :(得分:1)

这应该让你开始:

RewriteEngine On

# Replace underscores with hyphens in all requests ending in .html.
# The rule replaces one char at a time and is repeated until no matches are found.
RewriteCond "%{QUERY_STRING}" "\.html$" [NC]
RewriteRule ^([^_]*)_(.*) $1-$2 [N]

# Capture the file name without extension inside braces
# Use a lower-case mapping on the file name.
# Destination is /songs/filename
RewriteMap lc int:tolower 
RewriteRule ^.*/(.*)\.html$ /songs/${lc:$1} [NC, R=301]