隐藏PHP扩展和放大器;力斜杠 [英] hide .php extension & force trailing slash

查看:137
本文介绍了隐藏PHP扩展和放大器;力斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图删除PHP扩展和力量,在网址的最后结尾的斜杠。

我找到了一个答案做到这一点正是我需要(<一href="http://stackoverflow.com/questions/19749033/htaccess-trouble-with-hiding-file-extension-and-forcing-trailing-slash">.htaccess与隐藏文件扩展名,并迫使斜杠),但它不工作的麻烦,我相信这是因为我是如何迫使我的网站更改文档根目录。

由于我没有使用虚拟主机,我必须使用htaccess的改变我的文档根目录,我已经通过应用以下code来实现它。

 的RewriteCond%{HTTP_HOST} ^ site.co $ [NC]
重写规则^(。*)$ http://www.site.co/$1 [R = 301,L]
的RewriteCond%{REQUEST_URI}!Webroot公司/
重写规则(。*)/ Webroot公司/ $ 1 [L]
 

这是我已经能够唯一的工作解决方案适用于隐藏PHP扩展是低于code。

删除扩展名为.php(仅适用于工作液)

 的RewriteCond%{} THE_REQUEST ^ \ W + \ /(。*)\。PHP(\ *?)?\ HTTP /
重写规则^ HTTP://%{HTTP_HOST} /%1 [R = 301]

的RewriteCond%{} REQUEST_FILENAME -f .PHP
重写规则。* $ 0.php
 

这code是在我在上面已经挂了线,但它并不在我的网站工作,我相信这是由于文档根目录。

  RewriteEngine叙述上

#强制斜线添加
的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME!-d
重写规则^(。+?[^ /])/ $ 1 / [R = 301,L]

#的.php分机隐藏
的RewriteCond%{} REQUEST_FILENAME!-d
的RewriteCond%{DOCUMENT_ROOT} / $ 1.PHP -f
重写规则^(。+?)/?$ /$1.php [L]
 

解决方案

请尝试使用不同的排序这些修改后的规则:

  RewriteEngine叙述上

#主机名前加WWW
的RewriteCond%{HTTP_HOST} ^网站\ .CO $ [NC]
重写规则^ HTTP://www.% {HTTP_HOST}%{REQUEST_URI} [R = 302,L,NE]

#如果在文章页面,获得蛞蝓使之成为友好的URL
?的RewriteCond%{THE_REQUEST} \ S /条\ .PHP \ article_uid =([^&功放;] +)及article_title =([^&放大器; \] +)
重写规则^ /条/ 1%/ 2%/? [L,R = 302,NE]

#如果请求与PHP页面然后删除扩展
的RewriteCond%{THE_REQUEST} \ S / +(。+?)\ PHP [\ S?] [NC]
重写规则^ /%1 / [R = 302,L,NE]

#强制斜线添加
的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME!-d
的RewriteCond%{THE_REQUEST} \ S / +([^] + [^ /])[\ S?] [NC]
重写规则^ /%1 / [R = 302,L]

#允许页面方向改变蛞蝓到SEO友好的URL
的RewriteCond%{} REQUEST_FILENAME!-d
的RewriteCond%{} REQUEST_FILENAME!-f
重写规则?(?:^ | /)条/([^ /] +)/([^ /] +)/ $ /webroot/article.php?article_uid=$1&article_title=$2 [L,QSA,NC]

#默默地改写到Web根目录
的RewriteCond%{REQUEST_URI}!/ Webroot公司/ [NC]
重写规则^ /根目录%{REQUEST_URI} [L]

#的.php分机隐藏
的RewriteCond%{} REQUEST_FILENAME!-d
的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME -f .PHP
重写规则^(。+?)/?$ $ 1.PHP [L]
 

I've been trying to delete the .php extension and force trailing slashes in the end of the URLs.

I've found an answer to do it exactly what I need at (.htaccess trouble with hiding file extension and forcing trailing slash) but it does not work, I believe it's due to how I am forcing my website to change the document root.

As I do not have access to Virtual Hosts I must change my document root using htaccess, and I have accomplished it by applying the following code..

RewriteCond %{HTTP_HOST} ^site.co$ [NC]
RewriteRule ^(.*)$ http://www.site.co/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !webroot/
RewriteRule (.*) /webroot/$1 [L]

The only working solution that I've been able to apply to hide the .php extension is with the code below.

removing .php extension(only working solution)

RewriteCond %{THE_REQUEST} ^\w+\ /(.*)\.php(\?.*)?\ HTTP/
RewriteRule ^ http://%{HTTP_HOST}/%1 [R=301]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .* $0.php

This code is in the thread that I have linked above, but it does not work on my website as I believe it's due to the document root.

RewriteEngine on

# Forces a trailing slash to be added
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?[^/])$ /$1/ [R=301,L]

# .php ext hiding
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ /$1.php [L]

解决方案

Try these modified rules with different ordering:

RewriteEngine on

# add www before hostname
RewriteCond %{HTTP_HOST} ^site\.co$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=302,L,NE]

# if on article page, get slugs and make into friendly url
RewriteCond %{THE_REQUEST} \s/article\.php\?article_uid=([^&]+)&article_title=([^&\ ]+)
RewriteRule ^ /article/%1/%2/? [L,R=302,NE]

# if page with .php is requested then remove the extension
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1/ [R=302,L,NE]

# Force a trailing slash to be added
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s/+([^.]+?[^/.])[\s?] [NC]
RewriteRule ^ /%1/ [R=302,L]

# allow page direction to change the slugs into friendly seo URL
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule (?:^|/)article/([^/]+)/([^/]+)/?$ /webroot/article.php?article_uid=$1&article_title=$2 [L,QSA,NC]

# silently rewrite to webroot
RewriteCond %{REQUEST_URI} !/webroot/ [NC]
RewriteRule ^ /webroot%{REQUEST_URI} [L]

# .php ext hiding
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

这篇关于隐藏PHP扩展和放大器;力斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆