简体   繁体   中英

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.

My website has a lot of urls that end with .php at varying folder depths across the site. I would like all the urls to be site.net/folder/folder/filename/ regardless of the amount of subfolders.

My problem is some of my forms use POST and GET request. 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.

When the .php urls are wiped out I am not able to find the upload scripts either. Can the mod-rewrite rules be structured to get the php extensions out without obstructing my 'worker' scripts (upload.php, processcontactinfo.php, etc)? 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/. 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.

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

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 .

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. Note that mod_dir includes a trailing slash because of a serious information disclosure security issue. . 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 . As for the last bit, you're again running into the problem where you're trying to apply a RewriteCond to multiple RewriteRule 's. Both those conditions only apply to RewriteRule ^(.*[^/])$ /$1.php [L] , the last rule gets applied with no conditions at all. 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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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