(htaccess)将php文件的英文名称重写为荷兰语等价物

时间:2013-05-01 11:37:44

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

我想将php文件的英文名称改写为荷兰语等同物。

例如: someurl.com/news.php?readmore=4#comments 应该变为 someurl.com/nieuws.php?leesmeer=4#kommentaar 。应该执行news.php中的代码,但是nieuws.php应该在参数应该起作用的url中。

我尝试了几个htaccess示例,但我无法让它工作。

任何帮助都将不胜感激。

编辑:从以下答案和最终解决方案开始的工作进度。

RewriteCond %{QUERY_STRING} ^readmore=(.*)$
RewriteRule ^news.php$ nieuws.php?leesmeer=%1 [R=301,L]

RewriteCond %{QUERY_STRING} !^norewrite[\w\W]*$
RewriteRule ^news.php$ nieuws.php [R=301,L]
RewriteRule ^nieuws.php$ news.php?norewrite [QSA]

RewriteCond %{QUERY_STRING} !^norewrite[\w\W]*$
RewriteRule ^search.php$ zoeken.php [R=301,L]
RewriteRule ^zoeken.php$ search.php?norewrite [QSA]

2 个答案:

答案 0 :(得分:2)

# make sure rewrite is activ
RewriteEngine On 

# Rewrite a request for nieuws.php to news.php
RewriteRule ^nieuws.php$  news.php

应该做的伎俩。

Instad你可以将所有请求发送到index.php并在那里解析它们:

## Redirect everything to http://hostname/?path=requested/path
RewriteEngine On

RewriteRule ^([\w\W]*)$  index.php?path=$1 [QSA]

[QSA]确保您也获得原始的获取参数。

现在您必须在index.php和include请求的页面中解析$ _GET ['路径']中的请求。

例如:

if ($_GET['path'] == 'nieuws.php') {
   include 'news.php';
} else if (empty($_GET['path'])) {
   echo "HOME";
}

如果您想让用户始终在其地址栏中看到nieuws.php,即使他请求news.php,您也可以尝试以下操作:

RewriteEngine On

# Redirect news.php to nieuws.php if and only if the request comes from the client
# (suppose the client didn't set ?norewrite.)
RewriteCond %{QUERY_STRING} !^norewrite[\w\W]*$
RewriteRule ^news.php$ nieuws.php [R=301,L]

# Send news.php if nieuws.php was requested and prevent news.php from being redirected
# to back to nieuws.php by the rule above.
RewriteRule ^nieuws.php$ news.php?norewrite [L,QSA]

(R = 301表示发送"永久移动"重定向到客户端,L表示此规则匹配后停止重写)

只需要使用norewrite(你可以使用别的东西)来避免在新闻和新闻之间重写的重写循环。

要翻译GET参数,您可以在上面代码的第一行之前尝试以下代码:

RewriteCond %{QUERY_STRING} ^readmore=(.*)$
RewriteRule ^news.php$ nieuws.php?lesseer=%1 [R=301,L]

在网址中#后面的内容无法在.htaccess中更改,因为它们根本不会发送到服务器。改变它们的唯一机会是使用JavaScript。 (这里有很多关于在JavaScript中操作它们的问题)

答案 1 :(得分:0)

尝试:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /news\.php
RewriteRule ^ /nieuws.php [L,R=301,QSA]

RewriteRule ^nieuws\.php$ /news.php [L,QSA]