2

I would like to rewrite the English names of php files to their Dutch equivalents.

For example: someurl.com/news.php?readmore=4#comments should become someurl.com/nieuws.php?leesmeer=4#kommentaar. The code from news.php should be executed but nieuws.php should be in the url the arguments should function as well.

I tried several htaccess examples but I can't get it to work.

Any help would be appreciated.

Edit: Working progress from answers below and final solution.

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]
4

2 回答 2

2
# make sure rewrite is activ
RewriteEngine On 

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

Should do the trick.

Instad you could send all requests to an index.php and parse them there:

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

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

[QSA] makes sure you get the original get arguments too.

Now you have to parse the request in $_GET['path'] in you index.php and include the requested page.

eg:

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

if you want to make make the user always sees nieuws.php in its address bar, even if he requested news.php, you could try the following:

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 means send a "moved permanently" redirect to the client, L means stop rewriting after this rule matched)

The hole thing with norewrite (you could use something else instead) is only needed to avoid an endles loop of rewriting between news and nieuws.

To translate the GET arguments, you can try the following code before the first line of the above code:

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

Things after a the # in an url can't be changed in .htaccess, since they aren't send to the server at all. The only chance to change them is using JavaScript. (See lots of question here on manipulating them within JavaScript)

于 2013-05-01T12:33:56.440 回答
0

Try:

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

RewriteRule ^nieuws\.php$ /news.php [L,QSA]
于 2013-05-01T16:48:31.323 回答