I currently have a URL that looks like this:
www.mysite.com/folder-name/page-name.php
but I need it to look like this:
www.mysite.com/folder-name/page-name/
I'm after a regular expression for the .htaccess that goes in "folder-name" and targets only the folder it is in. It needs to remove the .php extension and redirect to a url with a trailing slash at the end.
Also if the url is www.mysite.com/folder-name/
I need the the expression to be ignored to avoid a // at the end. I've had a look but couldn't find a solution for just a particular folder. Just beginning with PHP and regex's are blowing my mind!.
I've found code like this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+.php
RewriteRule (.*).php$ /$1/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)/$ $1.php [L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .*[^/]$ $0/ [L,R=301]
but it returns urls unto this:
www.mysite.com/var/www/vhosts/mysite.com/httpdocs/folder_name/page_name/
The reason why you are getting the path like that is because apache is guessing whether your rule's target ( $0/
) is a file path or URL path, and it's guessing incorrectly that it's a file path. You need to either provide a rewrite base:
RewriteBase /
or make your targets absolute URI's
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)/$ /$1.php [L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .*[^/]$ /$0/ [L,R=301]
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.