PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, March 14, 2022

[FIXED] .htaccess RewriteRule skip URI that has query params

 March 14, 2022     .htaccess, apache, mamp, mod-rewrite     No comments   

Issue

I'm attempting to re-route all traffic on a local wordpress instance (ran by MAMP) from

localhost:8080/mydomain to localhost:8080/mydomain/dashboard with an exception. I don't want to re-route the URI if it has query parameters like this:

localhost:8080/mydomain/?post_type=ignition_product&p=23&preview=1 . The query paramater p will change.

I've read this In Depth Guide to mod-rewrite, but I'm still having issues.

I have made an attempt but I think it's brutally wrong:

// done by wordpress
# BEGIN WordPress
<IfModule mod_rewrite.c>

RewriteEngine On
RewriteBase /mydomain/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /fundblack/index.php [L]

// added by me
RewriteCond ...need help here trying to ignore if there are query params... 
RewriteRule ^mydomain/?(\w+)$ - [L]

RewriteRule ^mydomain/$ mydomain/dashboard
</IfModule>

# END WordPress

Update

Albert's answer worked perfectly. Now I'm actually hosting the website on a server. The domain address is like this:

subdomain.subdomain.mydomain.com

is simply did this, but it's not working:

RewriteBase /
RewriteCond %{REQUEST_URI} !^/dashboard.* [NC]
RewriteCond %{REQUEST_URI}%{QUERY_STRING} !^/post_type=ignition_product&p=.*&preview=1$ [NC]
RewriteRule ^(.*)$ dashboard/$1 [R=301,L]

what could be the problem?


Solution

You don't two rewrite rules, just two conditions in the .htaccess inside /mydomain/ folder:

RewriteBase /mydomain/
RewriteCond %{REQUEST_URI} !^/mydomain/dashboard.* [NC]
RewriteCond %{REQUEST_URI}%{QUERY_STRING} !^/mydomain/post_type=ignition_product&p=.*&preview=1$ [NC]
RewriteRule ^(.*)$ dashboard/$1 [R=301,L]

The first condition prevents infinite loops if you try to access directly localhost:8080/mydomain/dashboard/anything and the second condition exclude URLs with that query string for any value of the p parameter (note that the missing ? is not a typo because it's not included in the composed %{REQUEST_URI}%{QUERY_STRING}).

Also you don't need to use /mydomain/ in the rule since you previously have RewriteBase /mydomain/.


Edit: For putting it in your final domain remove the RewriteBase since is not needed and put the / directly in the rule (I use it in the first answer because you have it defined in your original .htaccess):

RewriteCond %{REQUEST_URI} !^/dashboard.* [NC]
RewriteCond %{REQUEST_URI}%{QUERY_STRING} !^/post_type=ignition_product&p=.*&preview=1$ [NC]
RewriteRule ^(.*)$ /dashboard/$1 [R=301,L]


Answered By - Alberto Martinez
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing