.htaccess Rewrite: how to ignore sub pages

Viewed 31

I have WordPress pages like

https://example.com/mainpage

https://example.com/mainpage/sub-page1

https://example.com/mainpage/sub-page2

https://example.com/mainpage/sub-page3

I have written a RewriteRule just for mainpage

RewriteRule ^mainpage$ mainpage.php [L,QSA]

also tried

RewriteCond %{REQUEST_URI} !^/mainpage/sub-page1
RewriteRule ^mainpage$ mainpage.php [L,QSA]

and

RewriteCond %{REQUEST_URI} !^/mainpage/.*$
RewriteRule ^mainpage$ mainpage.php [L,QSA]

but all the sub-pages also end up in mainpage.php instead of loading the WordPress pages. How can I stop subpages loading mainpage.php

Any help would be appreciated. Thank you

2 Answers

Remove $ from the pattern, and add / and write it like this RewriteRule ^mainpage/ mainpage.php [L,QSA]

Hopefully, this should ignore sub-pages.

My page was https://example.com/mainpage.

  1. I changed the filename to "main_page.php" instead of mainpage.php
  2. Added the rewiterule RewriteRule ^mainpage$ main_page.php [L,QSA]

These changes worked for me.

Related