htaccess redirect if URL contains a certain string

前端 未结 4 1381
挽巷
挽巷 2020-11-30 00:35

How would I write a .htaccess redirect rule if the URL contains a certain word?

e.g. if it contains foobar then redirect to index.php

相关标签:
4条回答
  • 2020-11-30 01:19
    RewriteCond %{REQUEST_URI} foobar
    RewriteRule .* index.php
    

    or some variant thereof.

    0 讨论(0)
  • 2020-11-30 01:19

    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.

    To redirect example.com/foo/bar to /index.php if the uri contains bar anywhere in the uri string , you can use this :

    RewriteEngine on
    
    RewriteCond %{REQUEST_URI} bar
    RewriteRule ^ /index.php [L,R]
    
    0 讨论(0)
  • 2020-11-30 01:27
    RewriteRule ^(.*)foobar(.*)$ http://www.example.com/index.php [L,R=301]
    

    (No space inside your website)

    0 讨论(0)
  • 2020-11-30 01:35
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^/foobar/i$ index.php [NE,L]
    
    0 讨论(0)
提交回复
热议问题