search

Home  >  Q&A  >  body text

Regular expression - apache .htaccess URL redirect regular expression

I want to direct all mobile traffic to the original access URL/amp
For example:
When the mobile phone accesses http://example.com, it will be directed to http://example.com/amp
When the mobile phone accesses http://example.com/123, it will be directed to http://example.com/123/amp

I need to do this through the apache server, so I use .htaccess for redirection
The following .htaccess settings can help me do it

RewriteEngine On
RewriteCond %{REQUEST_URI} !/amp$ [NC]
RewriteCond %{HTTP_USER_AGENT} (android|blackberry|googlebot\-mobile|iemobile|iphone|ipod|\#opera\ mobile|palmos|webos) [NC]
RewriteRule ^([a-zA-Z0-9-]+)([\/]*)$ http://7abbd517.ngrok.io//amp [L,R=302]

http://example.com/123 leads to http://example.com/123/amp
But there is no way to help me achieve the redirection of the homepage
That is to say
「X」http ://example.com cannot go to http://example.com/amp

How should I rewrite it to achieve my original goal?

巴扎黑巴扎黑2714 days ago1030

reply all(1)I'll reply

  • 伊谢尔伦

    伊谢尔伦2017-06-28 09:29:47

    Since the plus sign after [a-zA-Z0-9-] means matching at least 1 element, so since http://example.com cannot match one element, + is changed to * that is But, in addition, there is no need to write the square brackets outside [/].

    reply
    0
  • Cancelreply