将 URL 从复数改写为单数
Rewrite URL from plural to singular
如何将所有后跟 /<integer>
的复数形式的 URL 重定向到单数名称以及整数作为参数。有关示例,请参见下文。理想情况下,“用户”不需要硬编码,“供应商”将以相同的方式重定向到“供应商”。请注意,我没有使用任何服务器代码(即 PHP 等)。
users.html(不是这个页面是复数)
<a href="/users/1">John Doe</a> <!-- should redirect to user.html?id=1 -->
<a href="/users/2">Jan Doe</a> <!-- should redirect to user.html?id=2 -->
<a href="/users/3">Baby Doe</a> <!-- should redirect to user.html?id=3 -->
当前配置如下
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName admin.facdocs.example.net
DocumentRoot /var/www/facdocs/frontends/admin/public
<Directory "/var/www/facdocs/frontends/admin/public">
#Options Indexes FollowSymLinks MultiViews
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
allow from all
RewriteEngine On
LogLevel info rewrite:trace3
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z]+)s/(\d+)/?$ .html?id= [L]
RewriteBase /
RewriteCond %{REQUEST_URI} !^.*\.html$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ %{REQUEST_FILENAME}.html
</Directory>
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/api.example.net/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/api.example.net/privkey.pem
SSLCertificateChainFile /etc/letsencrypt/live/api.example.net/chain.pem
</VirtualHost>
</IfModule>
要处理复数 仅 作为以 s
结尾的单词,并且仅当存在对应的 .html
时,您可以匹配为:
RewriteEngine On
# The actual file does not exist already as a file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Alpha excluding final `s` captures in
# Numeric value in with optional trailing slash
RewriteRule ^([A-Za-z]+)s/(\d+)/?$ .html?id= [L]
复数单词(以 s
结尾)没有相应的单数 .html
将导致 404。
注意:以上假定 <Directory>
或 .htaccess
上下文。如果将它放在服务器级别或 <Location>
,请使用前导斜杠
RewriteRule ^/([A-Za-z]+)s...
并且这还假定输入复数和对应的 .html
之间区分大小写。
如何将所有后跟 /<integer>
的复数形式的 URL 重定向到单数名称以及整数作为参数。有关示例,请参见下文。理想情况下,“用户”不需要硬编码,“供应商”将以相同的方式重定向到“供应商”。请注意,我没有使用任何服务器代码(即 PHP 等)。
users.html(不是这个页面是复数)
<a href="/users/1">John Doe</a> <!-- should redirect to user.html?id=1 -->
<a href="/users/2">Jan Doe</a> <!-- should redirect to user.html?id=2 -->
<a href="/users/3">Baby Doe</a> <!-- should redirect to user.html?id=3 -->
当前配置如下
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName admin.facdocs.example.net
DocumentRoot /var/www/facdocs/frontends/admin/public
<Directory "/var/www/facdocs/frontends/admin/public">
#Options Indexes FollowSymLinks MultiViews
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
allow from all
RewriteEngine On
LogLevel info rewrite:trace3
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z]+)s/(\d+)/?$ .html?id= [L]
RewriteBase /
RewriteCond %{REQUEST_URI} !^.*\.html$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ %{REQUEST_FILENAME}.html
</Directory>
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/api.example.net/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/api.example.net/privkey.pem
SSLCertificateChainFile /etc/letsencrypt/live/api.example.net/chain.pem
</VirtualHost>
</IfModule>
要处理复数 仅 作为以 s
结尾的单词,并且仅当存在对应的 .html
时,您可以匹配为:
RewriteEngine On
# The actual file does not exist already as a file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Alpha excluding final `s` captures in
# Numeric value in with optional trailing slash
RewriteRule ^([A-Za-z]+)s/(\d+)/?$ .html?id= [L]
复数单词(以 s
结尾)没有相应的单数 .html
将导致 404。
注意:以上假定 <Directory>
或 .htaccess
上下文。如果将它放在服务器级别或 <Location>
,请使用前导斜杠
RewriteRule ^/([A-Za-z]+)s...
并且这还假定输入复数和对应的 .html
之间区分大小写。