skip to Main Content

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

2

Answers


  1. Based on your shown samples, could you please try following. Please make sure to clear your browser cache before testing your URLs.

    RewriteEngine ON
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^Maintenance ?q=pageX [NC,L]
    
    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search