Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
164 views
in Technique[技术] by (71.8m points)

httpd (apache) - rewrite page -> query parameter

I have a website containing a directory and pages. Let's call the directory 'Maintenance' and pages 'page1.html,...'

I want to redirect each file under the directory to query parameter such as '/?q=pageX'.

<VirtualHost *:80>
ServerName      myServer.com
ServerAlias     www.myServer.com
DocumentRoot    "/var/www/html"    

RewriteEngine on
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]

RewriteRule ^(/Maintenance/[^/]+).html$ /?q=MyPage [R=301,L]

RewriteRule "^(.*).ht$" "index.html/?q=$1 [NC,L,QSA,CO=RewriteRule;01;https://www.myServer.com;30/;SameSite=None;Secure]"
Redirect permanent /(.*) https://%{SERVER_NAME}/$1

I've been trying ModeRewrite w/ various setups, read lots of 'how to' but nothing works for me.

Help will be most appreciated.

Thanks, John

question from:https://stackoverflow.com/questions/65894555/httpd-apache-rewrite-page-query-parameter

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use these rules in 2 VirtualHost sections:

<VirtualHost *:80>
ServerName      myServer.com
ServerAlias     www.myServer.com
DocumentRoot    "/var/www/html"

RewriteEngine on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
</VirtualHost>

<VirtualHost *:443>
ServerName      myServer.com
ServerAlias     www.myServer.com
DocumentRoot    "/var/www/html"

RewriteRule ^/Maintenance/([^/]+).html?$ /?q=$1 [QSA,NC,L]

RewriteRule ^(.*).ht$ /?q=$1 [NC,L,QSA,CO=RewriteRule;01;https://www.myServer.com;30/;SameSite=None;Secure]
</VirtualHost>

It is important to keep rewrite rules in VirtualHost *:443 because port 80 one is just redirecting all the traffic to https.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...