简体   繁体   English

从网址中删除PHP并强制使用斜杠

[英]Removing php from urls and force trailing slash

I have asked this question before and used bits and pieces of code I did not understand and now its all falling apart. 我之前曾问过这个问题,并使用了我不理解的点点滴滴的代码,现在一切都崩溃了。 Hopefully I can get my intentions across more precisely than before. 希望我能比以前更准确地传达我的意图。 I am trying to learn regular expressions but I can't seem to apply what I have learned to mod-rewrite techniques. 我正在尝试学习正则表达式,但似乎无法将学到的知识应用于mod-rewrite技术。

My website has a lot of urls that end with .php at varying folder depths across the site. 我的网站上有很多以.php结尾的网址,它们在整个网站上的文件夹深度不同。 I would like all the urls to be site.net/folder/folder/filename/ regardless of the amount of subfolders. 我希望所有的URL都是site.net/folder/folder/filename/而不site.net/folder/folder/filename/文件夹的数量如何。

My problem is some of my forms use POST and GET request. 我的问题是我的某些表单使用POST和GET请求。 So when a user clicks upload to upload a file I usually set the action part of a form to a .php file, for eg site.net/folder/script/upload.php or site.net/folder/folder/scripts/upload.php usually handles the uploading of files before it redirects to xyz page. 因此,当用户单击“上传”以上传文件时,我通常将表单的操作部分设置为.php文件,例如site.net/folder/script/upload.phpsite.net/folder/folder/scripts/upload.php通常在重定向到xyz页面之前处理文件的上传。

When the .php urls are wiped out I am not able to find the upload scripts either. 删除.php网址后,我也无法找到上传脚本。 Can the mod-rewrite rules be structured to get the php extensions out without obstructing my 'worker' scripts (upload.php, processcontactinfo.php, etc)? 是否可以对mod-rewrite规则进行结构化,以获取php扩展名而又不妨碍我的“ worker”脚本(upload.php,processcontactinfo.php等)? EDIT 编辑

This is what my script looks like so far. 到目前为止,这就是我的脚本。

Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} ^POST\ /([^.\ ]+\.)+php(\?[^\ ]*)?\ HTTP
RewriteCond %{THE_REQUEST} ^GET\ /([^.\ ]+\.)+php(\?[^\ ]*)?\ HTTP
RewriteRule ^(.+)\.php$ http://www.site.net/$1 [R=301,L]
RewriteRule ^([^/]+/)*index/?$ http://www.site.net/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ http://www.site.net/$1 [R=301,L]
RewriteCond $1 !^([^.]+\.)+([a-z0-9]+)$ [NC]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*[^/])$ /$1.php [L]
RewriteRule (.*)/$ $1.php [L]

I have tried different variations, but like I said I am not good at reading or creating the regular expressions. 我尝试了各种变体,但是就像我说的那样,我不擅长阅读或创建正则表达式。 So it is mostly a trial and error effort at best. 因此,充其量这主要是反复试验的结果。 This allows me to access file files via site.net/folder/filename/ but it won't change a request from site/folder/filename.php to site.net/folder/filename/. 这使我可以通过site.net/folder/filename/访问文件文件,但不会将请求从site / folder / filename.php更改为site.net/folder/filename/。 Also at this point the various scripts cannot be found. 同样在这一点上,找不到各种脚本。

I hope this code will help you. 希望这段代码对您有所帮助。 you have to write this code on your .htaccess file. 您必须在.htaccess文件中编写此代码。

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

I need it to treat the file as a php file but don't show the extension 我需要将其视为php文件,但不显示扩展名

Then you need to use this: 然后,您需要使用以下代码:

RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{THE_REQUESTI} ^(GET|POST|HEAD)\ /(.+)\.php($|\?|\ )
RewriteRule ^ /%2 [L,R=301]

Your original rules were close, except that conditions are always ANDed together, so there's no way the first two conditions can both be true. 您的原始规则很接近,除了条件总是与在一起,所以前两个条件都不能成立。 Also, conditions only apply to the immediately following RewriteRule , so your second RewriteRule gets applied EVERY TIME . 同样,条件仅适用于紧随其后的RewriteRule ,因此您的第二个RewriteRule会被EVERY TIME应用。

As for the rest of your rules: 至于其余的规则:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ http://www.site.net/$1 [R=301,L]

This may cause problems, since mod_dir does exactly the opposite , you may end up with a redirect loop. 这可能会引起问题,因为mod_dir的作用恰恰相反 ,您可能会遇到重定向循环。 Note that mod_dir includes a trailing slash because of a serious information disclosure security issue. 请注意,由于严重的信息泄露安全问题 ,mod_dir包含斜杠 . So if you want to use the above rule, include a: 因此,如果要使用上述规则,请包括:

DirectorySlash Off

to turn off mod_dir redirecting the browser to a URL with the trailing slash . 关闭mod_dir,将浏览器重定向到带有斜杠的URL。 As for the last bit, you're again running into the problem where you're trying to apply a RewriteCond to multiple RewriteRule 's. 至于最后一点,您再次遇到了将RewriteCond应用于多个RewriteRule的问题。 Both those conditions only apply to RewriteRule ^(.*[^/])$ /$1.php [L] , the last rule gets applied with no conditions at all. 这两个条件仅适用于RewriteRule ^(.*[^/])$ /$1.php [L] ,最后一个规则完全没有条件应用。 But if looks like you're trying to do: 但是,如果您似乎要尝试这样做:

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /$1.php [L]

RewriteCond %{REQUEST_URI} ^/(.*)/$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1.php [L]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM