How would I write a .htaccess
redirect rule if the URL contains a certain word?
如果URL包含某个单词,我该如何编写.htaccess重定向规则?
e.g. if it contains foobar
then redirect to index.php
例如如果它包含foobar然后重定向到index.php
4 个解决方案
#2
14
RewriteRule ^(.*)foobar(.*)$ http://www.example.com/index.php [L,R=301]
(No space inside your website)
(您的网站内没有空间)
#3
1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/foobar/i$ index.php [NE,L]
#4
1
If url contains a certen string, redirect to index.php . You need to match against the %{REQUEST_URI} variable to check if the url contains a certen string.
如果url包含certen字符串,则重定向到index.php。您需要匹配%{REQUEST_URI}变量以检查URL是否包含certen字符串。
To redirect example.com/foo/bar to /index.php if the uri contains bar anywhere in the uri string , you can use this :
要将example.com/foo/bar重定向到/index.php,如果uri在uri字符串中的任何位置包含bar,则可以使用:
RewriteEngine on
RewriteCond %{REQUEST_URI} bar
RewriteRule ^ /index.php [L,R]
#1
#2
14
RewriteRule ^(.*)foobar(.*)$ http://www.example.com/index.php [L,R=301]
(No space inside your website)
(您的网站内没有空间)
#3
1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/foobar/i$ index.php [NE,L]
#4
1
If url contains a certen string, redirect to index.php . You need to match against the %{REQUEST_URI} variable to check if the url contains a certen string.
如果url包含certen字符串,则重定向到index.php。您需要匹配%{REQUEST_URI}变量以检查URL是否包含certen字符串。
To redirect example.com/foo/bar to /index.php if the uri contains bar anywhere in the uri string , you can use this :
要将example.com/foo/bar重定向到/index.php,如果uri在uri字符串中的任何位置包含bar,则可以使用:
RewriteEngine on
RewriteCond %{REQUEST_URI} bar
RewriteRule ^ /index.php [L,R]