I've got the following rules to work which:
- only act on files that exist
- exclude any files that contain images|js|css in their uri
- add trailing slash to request uri
Rewrite rules:
RewriteEngine on
DirectorySlash Off
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/(images|js|css)$
RewriteRule ^(.*[^/.])$ /$1/ [R=301,L]
I now need to correctly redirect my home uri's like so:
http://www.example.com/sitemap/
-> http://www.example.com/index.php?page=sitemap
I've tried the following approach:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(开发者_如何学JAVA.*[^/.])$ index.php?page=$1 [R=301,L,NC]
But I get a page not found, presumably because $1 is being fed something with a slash in it. I thought [^/]
would remove it but apparently not.
Could someone explain where I am going wrong here please?
Use this rule -- it will rewrite /sitemap/
into /index.php?page=sitemap
:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ /index.php?page=$1 [QSA,L]
Put it into .htaccess into website root folder. If placed elsewhere it need to be tweaked a bit.
URL will stay the same. Existing query string will be preserved.
The trailing slash
/
must be present (i.e./sitemap
will not trigger this rule).It will only rewrite if there is no such folder or file (i.e. if you have a folder named
sitemap
in your website root folder then no rewrite will occur).It will only work for 1-folder deep URLs (e.g.
/sitemap/
,/help/
,/user-account/
etc). It will not work for 2 or more folders in path (e.g./account/history/
).
RE: this line: RewriteCond %{REQUEST_URI} !/(images|js|css)$
.
You said you want "exclude any files that contain images|js|css in their uri". Unfortunately the above pattern work differently -- it will match /something/css
but will not match /css/something
or /something/file.css
.
If you want to match images|js|css ANYWHERE in URL straight after a slash, then remove $
.
精彩评论