2016-10-01 52 views
1

哪种方法更适合重写URL重写URL的更好方法

(a)。使用htaccess的文件,并在使用正则表达式,用于重写规则

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

(B)。 使用htacess文件将每个url重定向到index.php,然后使用PHP进行进一步重定向。

Options +FollowSymLinks 
RewriteEngine On 

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

RewriteRule ^.*$ ./index.php 

,之后每个URL可以通过使用index.php$_SERVER['REQUEST_URI']处理,我们可以使用require()语句加载特定文件

哪种方法是安全性和速度的基础上更好吗?

回答

0

这种方法更加完善,使用下面的代码.htaccess文件,并确定要在你的脚本,如访问您的目录:JS,CSS或上传文件夹

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

,如果你想要把你的脚本你必须在倒数第二个发言的index.php之前定义的文件夹名称,我们的文件夹内

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

确保你没有把任何额外的空间,代号或输入其它方式重写模式将不会执行。

注意:还要确保从/ 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]