是否可以在.htaccess文件中重定向多个文件名,例如,我使用的文件如下:
/publications/publications-1.php
/publications/publications-2.php
/publications/publications-3.php我想将它们更改为新的文件名:
/publications/1.php
/publications/2.php
/publications/3.php发布于 2017-01-04 17:41:18
在您的php文件所在的目录中尝试如下所示,
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+).php$ publications-$1.php [L]发布于 2017-01-04 18:46:35
将htaccess文件添加到您的webroot目录中,并添加如下规则:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^publications/(.*)\.php$ /publications-$1.php [L,QSA]
</IfModule>
## Results
# publications/data.php => publications-data.php它将任何符合条件的请求映射到幕后下相应的php文件。如果您需要重定向,请将R标志附加到规则。
如果您需要一个更通用的规则来匹配第一个片段中的任何内容,请尝试执行以下操作:
RewriteRule ^(.*)/(.*)\.php$ /$1-$2.php [L,QSA]
## Results
# foo/bar.php => foo-bar.php最后要注意的一件事是:确保启用了apache的重写模块,并且允许htaccess文件(httpd.conf中的AllowOverride All):
# If enabled, outputs something like this:
# rewrite_module (shared)
apachectl -M | grep rewrite # It's apache2ctl on some platforms
# If not enabled; you can enable it using this on most platforms:
a2enmod rewrite
apachectl restarthttps://stackoverflow.com/questions/41460473
复制相似问题