2016-10-01 38 views
1

書き換えURLに適した方法はどれですかURL書き換えのためのより良い方法

(a)です。 (b)は、書き換えルールの

Options +FollowSymLinks 
RewriteEngine On 

RewriteCond %{SCRIPT_FILENAME} !-d 
RewriteCond %{SCRIPT_FILENAME} !-f 

RewriteRule ^users/(\d+)*$ ./profile.php?id=$1 
RewriteRule ^threads/(\d+)*$ ./thread.php?id=$1 

RewriteRule ^search/(.*)$ ./search.php?query=$1 

OR

のhtaccessファイルを使用し、その中に正規表現を使用します。 すべてのURLをindex.phpにリダイレクトするためのhtacessファイルを使用して、さらにリダイレクトするためにPHPを使用します。

Options +FollowSymLinks 
RewriteEngine On 

RewriteCond %{SCRIPT_FILENAME} !-d 
RewriteCond %{SCRIPT_FILENAME} !-f 

RewriteRule ^.*$ ./index.php 

、その後、すべてのURLが$_SERVER['REQUEST_URI']を使っindex.phpで扱うことができ、我々は方法は、セキュリティと速度に基づいて優れている負荷特定のファイル

ためrequire()ステートメントを使用することができますか?

答えて

0

この方法は、あなたの.htaccessファイルの以下のコードを使用して、より完璧であり、また、次のようなスクリプトでアクセスしたいあなたのディレクトリを定義します:JSを、CSSやアップロードフォルダ

RewriteCond $1 ^(index\.php|robots\.txt|favicon\.ico|public|assets|**data**|**live**|**css**|**js**|**images**). 

あなたの場合それ以外のモデルを書き換えるあなたは、あなたが余分なスペースを入れていないことを確認し、文字または入力します最後から二番目の文たちな

RewriteRule ^(.*)$ **anyFolderName**/index.php/$1 [PT,L] 

で/index.php前に、フォルダ名を定義する必要がフォルダ内のスクリプトを入れたいです実行されません。

注意:また、server/apache/apache modules/rewrite_moduleからrewrite_moduleを実行/確認してください。

<IfModule mod_rewrite.c> 
# Turn on URL rewriting 
RewriteEngine On 

# If your website begins from a folder e.g localhost/my_project then 
# you have to change it to: RewriteBase /my_project/ 
# If your site begins from the root e.g. example.local/ then 
# let it as it is 
RewriteBase/

# Protect application and system files from being viewed when the index.php is missing 
RewriteCond $1 ^(application|system|private|logs) 

# Rewrite to index.php/access_denied/URL 
RewriteRule ^(.*)$ index.php/access_denied/$1 [PT,L] 

# Allow these directories and files to be displayed directly: 
RewriteCond $1 ^(index\.php|robots\.txt|favicon\.ico|public|assets) 

# No rewriting 
RewriteRule ^(.*)$ - [PT,L] 

# Rewrite to index.php/URL 
RewriteRule ^(.*)$ /index.php/$1 [PT,L] 
</IfModule> 
0

要件によって異なります。これらのphpファイルがすでに作成されている場合は、最初の.htaccessを単一のルールにリファクタリングするとうまくいくはずです:

Options +FollowSymLinks 
RewriteEngine On 

RewriteCond %{SCRIPT_FILENAME} !-d 
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f 
RewriteRule ^([\w-]+)/(\d+)/?$ $1.php?id=$2 [L,QSA] 
関連する問題