Currently I have the following code to remove .php from the url
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
But I'd also like to replace the character _ in a filename with a / in the URL. I've tried doing this but I simply get some weird errors. Anyone know how to do this?
Since you stated that only zero or one underscores may be part of a requested file, this should work:
RewriteCond %{REQUEST_URI} \.php$ [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME}\.php !-f
RewriteRule .* - [S=2]
RewriteRule ^(.*)$ $1.php
RewriteRule ^([^_]+)_([^_]+)$ $1/$2 [L]
To apply both rules (not just the first one) only if all three preceding conditions are true you negate the conditions and apply a skip rule afterwards: [S=2]
means "skip the following 2 rules".
So if all of the new three conditions are false (and therefore their negation is true) the first rewrite appends .php
to all requests. The second rewrite changes all underscores to slashes and stops rewriting.
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.