I need to drop ?mfp= (and anything after it), provided the URL contains /designs/ in it. So for example, these..
https://www.example.com/designs/upload-your-own/?mfp=30o-color%5B112%5D&tag=hoodies
https://www.example.com/designs/upload-your-own/?mfp=60o-type%5B191%5D%2C30o-color%5B112%5D&tag=hoodies
https://www.example.com/designs/upload-your-own/?mfp=manufacturers%5B43%5D%2C60o-type%5B191%5D%2C30o-color%5B112%5D&tag=hoodies
https://www.example.com/designs/upload-your-own/?mfp=c-categories-0%5B305%5D&tag=hoodies
..will become:
https://www.example.com/designs/upload-your-own/
https://www.example.com/designs/upload-your-own/
https://www.example.com/designs/upload-your-own/
https://www.example.com/designs/upload-your-own/
But something like:
https://www.example.com/hoodies/?mfp=c-categories-0[305],manufacturers[43],60o-type[191],30o-color[112]
..will remain untouched (since it doesn't have /designs/ in the URL).
Thanks!
You can use this rule as first rule in your site root .htaccess:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^mfp= [NC]
RewriteRule ^designs/ %{REQUEST_URI}? [L,NC,R=301,NE]
# your remaining rules
? at the end of target URI will strip off any query string from original URI.
Apache mod_rewrite Introduction
Apache mod_rewrite Technical Details
Related
There are so many questions and answers regarding forwarding with .htaccess, but unfortunately, I can't find any example that works for my situation.
I need to silently (internally) forward the URL
https://www.example.com/track?id=X
to
https://www.example.com/index.php?route=account/order/info&order_id=X
where X can be any number.
Please advise on what is the correct rewrite rule for this scenario.
I am using Apache 2.4.6 on CentOS 7
You can do something like the following using mod_rewrite at the top of your root .htaccess file to internally rewrite the request:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^id=(\d+)
RewriteRule ^track$ index.php?route=account/order/info&order_id=%1 [L]
%1 is a backreference to the value of the id URL parameter, captured in the preceding RewriteCond directive.
You need to use a RewriteCond directive in order to match against the query string part of the URL, since the RewriteRule pattern matches the URL-path only.
I am trying to redirect one URL to another - both contain a query string, but prior to the string, there will be a variation on the pair of rules needed (ie. one rule for English, the other for French).
I have tried a few query string redirects that I found here, but I'm not sure how to approach this problem considering most suggest that the rule should be implemented into the document root htaccess file (below the language directories).
When the following URL is requested:
www.site.com/english/hr/jobs/job_posting.php?id=2019-A-01
I need it to forward to:
www.site.com/french/hr/jobs/job_posting.php?id="2019-A-02
Note that the first subdirectory (language requirement) and the last number (job ID in the data) have changed.
Try this rewrite:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^\/english\/hr\/jobs\/job_posting.php$
RewriteCond %{QUERY_STRING} ^id=2019-A-01$
RewriteRule ^(.*)$ /french/hr/jobs/job_posting.php?id=2019-A-02 [L,R=301]
I used this rule, but nothing happen when visiting pages starting with that string.
RewriteEngine on
RewriteRule ^index.php?controller=allproducts(.*)$ / [R=301,L]
What I'm doing wrong?
Query strings aren't considered to be a part of the URI, instead you need to use a condition search for the query string itself:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^controller=allproducts$
RewriteRule (.*) $1? [R,L]
This would redirect http://website.com/index.php?controller=allproducts to http://website.com/index.php
Or if you're on Apache 2.4 you can use the [QSD] flag which discards the query. You would need to setup backreferences if you wanted to keep part of the query or use an absolute URL on the substitution if you wan't everything to go to the homepage.
I am trying to get some help with this URL rewrite. I have already read multiple tutorials and documentation pages on how to do all this, but none of it makes sense to me. I also don't understand regular expression, so that doesn't help either. I have a semi working piece of code and just need help getting it working correctly.
I need: http://subdomain.domain.com?dl=2
to redirect to http://domain.com/subdomain.php?dl=2
The code I have is
RewriteEngine on
RewriteCond %{HTTP_HOST} ^subdomain.example\.com$ [NC]
RewriteCond %{REQUEST_URI} !page.php
RewriteRule ^(.+/)?([^/]*)$ page.php?dl=$2 [QSA,L,NC]
Which sends the variable but can't figure out the subdomain part. If anyone could please help me out, it would be greatly appreciated.
You need to check the QUERY_STRING as RewriteRule doesn't include it. In addition, your rule is not using the redirect flag R.
RewriteEngine on
# First, check for the subdomain
RewriteCond %{HTTP_HOST} ^subdomain.domain.com$ [NC]
# Then, check the query string - it should match digits (\d+)
RewriteCond %{QUERY_STRING} ^dl=\d+ [NC]
# Check if we are not at subdomain.php
# (This is redundant, but leaving it here in case you really need it)
RewriteCond %{REQUEST_URI} !^/subdomain.php
# If all the above conditions are true, match a root-request
# and redirect to domain.com/subdomain.php with the query string
# Note: You don't need to actually specify the query string
# in the destination URI - Apache will automatically
# hand it over upon redirect (using the R flag).
# The only time this is not the case is when you
# either add the QSD flag, or append the destination
# URI with a question mark.
RewriteRule ^$ http://domain.com/subdomain.php [R=302,L]
The above will redirect http://subdomain.domain.com/?dl=2 to http://domain.com/subdomain.php?dl=2. If you would like to make the redirect permanent and cached by browsers and search engines, change 302 to 301.
I have mod_rewrite working in a development environment.
This testing domain is using these rules in an .htaccess file:
Options +FollowSymLinks
Options +Indexes
RewriteEngine on
# deal with potential pre-rewrite spidered / bookmarked urls
RewriteRule ^clothes/index.php?pg=([0-9]+)$ /clothes/index$1.php [R=301,L]
# deal with actual urls
RewriteRule ^clothes/[0-9a-z-]+-pr([0-9]+).php$ /clothes/product.php?pid=$1 [L]
The 2nd Rule works fine. Entering http ://testdomain.dev/clothes/shirt-pr32.php is silently delivered content from http ://testdomain.dev/clothes/product.php?pid=32 ...which is as desired and expected!
However, assuming this was applied to a live site, one that had originally used paths such as: http ://testdomain.dev/clothes/product.php?pid=32, I'd like to redirect any incoming requests following the old pattern to the new urls ...which is what the 1st Rule was intended to do.
My problem is my testing server seems to ignore the 1st Rule and serves the page as requested (page loads but address bar remains at http ://testdomain.dev/clothes/product.php?pid=32)
Any assistance or enlightenment would be most graciously accepted!
You need to match the query string within a RewriteCond, then backreference that RewriteCond from the rule. The RewriteRule only matches against the path, not the query string.
Here's a related post I previously answered with a similar request: Mod_rewrite rewrite example.com/page.php?v1=abc&v2=def to example.com/abc/def
You can't match against the query string in a rewrite rule, you need to use the `%{QUERY_STRING} variable in a condition and use the % to backrefernce groupings. So instead of:
RewriteRule ^clothes/index.php?pg=([0-9]+)$ /clothes/index$1.php [R=301,L]
You'll need:
RewriteCond %{QUERY_STRING} ^pg=([0-9]+)
RewriteRule ^clothes/index.php$ /clothes/index%1.php? [R=301,L]