Regular expression - apache .htaccess URL redirect regular expression
巴扎黑
巴扎黑 2017-06-28 09:27:47
0
1
1049

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?

巴扎黑
巴扎黑

reply all(1)
伊谢尔伦

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 [/].

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template