将我的ErrorDocument Urls重写为更友好的URL

时间:2015-11-27 21:55:28

标签: php .htaccess mod-rewrite

这是我的.htaccess

Options +FollowSymlinks +Includes +SymLinksIfOwnerMatch +ExecCGI -MultiViews -Indexes
<files .htaccess>
   Order allow,deny
   Deny from all
</files>

ErrorDocument 400 https://agentc0re.com/brokebot/errorpages-headless-robot.html?errorcode=400
ErrorDocument 401 https://agentc0re.com/brokebot/errorpages-headless-robot.html?errorcode=401
ErrorDocument 403 https://agentc0re.com/brokebot/errorpages-headless-robot.html?errorcode=403
ErrorDocument 404 https://agentc0re.com/brokebot/errorpages-headless-robot.html?errorcode=404
ErrorDocument 405 https://agentc0re.com/brokebot/errorpages-headless-robot.html?errorcode=405
ErrorDocument 406 https://agentc0re.com/brokebot/errorpages-headless-robot.html?errorcode=406
ErrorDocument 407 https://agentc0re.com/brokebot/errorpages-headless-robot.html?errorcode=407
ErrorDocument 408 https://agentc0re.com/brokebot/errorpages-headless-robot.html?errorcode=408
ErrorDocument 409 https://agentc0re.com/brokebot/errorpages-headless-robot.html?errorcode=409
ErrorDocument 410 https://agentc0re.com/brokebot/errorpages-headless-robot.html?errorcode=410
ErrorDocument 411 https://agentc0re.com/brokebot/errorpages-headless-robot.html?errorcode=411
ErrorDocument 412 https://agentc0re.com/brokebot/errorpages-headless-robot.html?errorcode=412
ErrorDocument 413 https://agentc0re.com/brokebot/errorpages-headless-robot.html?errorcode=413
ErrorDocument 414 https://agentc0re.com/brokebot/errorpages-headless-robot.html?errorcode=414
ErrorDocument 415 https://agentc0re.com/brokebot/errorpages-headless-robot.html?errorcode=415
ErrorDocument 500 https://agentc0re.com/brokebot/errorpages-headless-robot.html?errorcode=500
ErrorDocument 501 https://agentc0re.com/brokebot/errorpages-headless-robot.html?errorcode=501
ErrorDocument 502 https://agentc0re.com/brokebot/errorpages-headless-robot.html?errorcode=502
ErrorDocument 503 https://agentc0re.com/brokebot/errorpages-headless-robot.html?errorcode=503
ErrorDocument 504 https://agentc0re.com/brokebot/errorpages-headless-robot.html?errorcode=504
ErrorDocument 505 https://agentc0re.com/brokebot/errorpages-headless-robot.html?errorcode=505

<IfModule mod_rewrite.c>
RewriteEngine on

### Force Site in SSL ###

RewriteCond %{HTTP_HOST} ^(www\.)?agentc0re\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://agentc0re.com/$1 [R,L]

### rewrite site for HTTPS or HTTP to be non WWW ###

#RewriteCond %{HTTPS} off
#RewriteCond %{HTTP_HOST} ^(www\.)?agentc0re.com$ [NC]
#RewriteRule ^(.*)$ http://agentc0re.com/$1 [R=301,L,NC]

#RewriteCond %{HTTPS} on
#RewriteCond %{HTTP_HOST} ^(www\.)?agentc0re.com$ [NC]
#RewriteRule ^(.*)$ https://agentc0re.com/$1 [R=301,L,NC]

### Remove /agentc0re/ from URL ###

RewriteCond %{HTTP_HOST} ^(www.)?agentc0re.com$ [NC]
RewriteCond %{REQUEST_URI} !^/agentc0re/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /agentc0re/$1 [L]
RewriteCond %{HTTP_HOST} ^(www.)?agentc0re.com$ [NC]
RewriteRule ^(/)?$ agentc0re/index.php [L]

### Rewrite the Error Codes to be more simple

RewriteCond %{HTTP_HOST} ^(www.)?agentc0re.com$ [NC]
RewriteCond %{REQUEST_URI} !^/brokebot/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /brokebot/$1 [L]
#RewriteRule ^([0-9]{3})/?$ /brokebot/errorpages-headless-robot.html?errorcode=$1 [L,NC]
RewriteCond %{HTTP_HOST} ^(www.)?agentc0re.com$ [NC]
RewriteRule ^/(.*)$ brokebot/errorpages-headless-robot.html?errorcode=$1 [L,NC]

</IfModule>

#
# If Apache is compiled with built in mod_deflade/GZIP support
# then GZIP Javascript, CSS, HTML and XML so they're sent to
# the client faster.
#
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE application/x-javascript text/css text/html text/xml
</IfModule>

我有我想要使用的所有ErrorDocument代码。但我想重写EG的网址:

https://agentc0re.com/brokebot/errorpages-headless-robot.html?errorcode=400
TO
https://agentc0re.com/400

我的.htaccess文件中的行

### Rewrite the Error Codes to be more simple

正如你所看到的,我一直在尝试的事情。但是我没有成功地完成任何工作。有人会介意帮我吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

将以下下面的HTTP添加到HTTPS重定向规则

 #Rewrite errorpages
 RewriteCond %{THE_REQUEST} /erropages-headless-robot\.html\?errorcode=([0-9]+) [NC]
RewriteRule ^ /%1? [NE,NC,L,R]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([0-9]+)$ /brokebot/erropages-headless-robot.html?errorcode=$1 [NC,L]
#End of error page rewrite

此代码将映射

 example.com/error_code

 /brokebot/erropages-headless-robot.html?errorcode=error_code

然后,您可以添加一个绝对路径“/ 404”,“/ 403”

,而不是将完整的URL添加到errodocumen路径中。

前:

ErrorDocument 404 /404
ErrorDocument 403 /403
ErrorDocument 410 /410