3

私はKohanaフレームワークを使用しています(ただし、この質問には関係ないと思います)。ページにはそのようにアクセスできます

http://www.example.com/articles/
http://www.example.com/index.php/articles/

現在、経験則として、私は通常、.htaccess を微調整して、ページへのアクセスを 1 つだけ許可し、他の一般的な方法を静かにリダイレクトします。

基本的に、上記の 1 番目の URL では、アドレスは実際には内部で 2 番目の例にリダイレクトされます。

私がやりたいことは、2 番目のタイプの URL を強制的に最初のタイプの URL に変えることです。私は .htaccess に自信がないことが多く、最初の試行で予期しない結果が発生します (場合によっては無限ループが発生するなど)。

これが私が思いついたものです

RewriteRule ^index\.php/(.*) $1 [NC,L,R=301]

誰かが私が間違っていることを教えてもらえますか? また、この問題に遭遇したことがある場合は、どのように解決しましたか?

編集

すべてのリダイレクトを調べることができるように、.htaccess 全体を投稿することにしました。

# Turn on URL rewriting
RewriteEngine On

# Installation directory
RewriteBase /~toberua/


# file not found page
    ErrorDocument 404 /404/
    ErrorDocument 403 /403/

# get people out of my directories
    Options -Indexes

# default page to load
    DirectoryIndex index.php

# add trailing slash if missing
    RewriteRule ^(([a-z0-9\-]+/)*[a-z0-9\-]+)$ $1/ [NC,R=301,L]

# redirect /favicon.ico requests
     RewriteCond %{REQUEST_URI} !^/images/layout/favicon\.ico [NC]
     RewriteCond %{REQUEST_URI} favicon\.(gif|ico|png|jpe?g) [NC]
     RewriteRule (.*) images/layout/favicon.ico [R=301,L]

# send /home back to TLD
     RewriteRule home/ $1  [NC,R=301,L]

# ensure there is no /index.php in the address bar
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php\ HTTP/
    RewriteRule ^(.*)index\.php$ $1 [R=301,L] # this was my attempt to stop /dir/index.php and make it simply /dir/

    RewriteRule ^index\.php/(.*) $1 [NS,NC,L,R=301]

# Protect application and system files from being viewed
RewriteRule ^(application|modules|system) - [F,L]

# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php/$0 [PT,L]
4

1 に答える 1

3

これを試して:

RewriteRule ^index\.php/(.*) $1 [NS,NC,L,R=301]

あなたが処理していないのは、すべてのリクエストが書き換えの対象であり、書き換えを行うとサブリクエストが生成されることです。これも書き換えの対象です。したがって、/articles を /index.php/articles に書き換えてしまい、そのサブリクエストで /index.php/articles を /articles に書き換えて、301 リダイレクトの新しいリクエストを何度も生成します。NS フラグを追加すると、このルールはサブリクエストで動作しなくなります。/articles -> /index.php/articles の書き換えで 301 も実行していない限り、問題は解決するはずです (ただし、それは狂気です)。

于 2009-05-25T01:29:58.287 に答える