.htaccess forward silently (internally) url containing question mark and variable - .htaccess

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.

Related

Redirect certain subfolders by removing the parameter question mark

I am using .htaccess to redirect certain subfolders of my domain, to remove the question mark to improve my URLs.
Currently my URLs are like this:
www.example.com/post/?sometitle
I am trying to remove the question mark, so it is the following URL:
www.example.com/post/sometitle
Currently I have the following code in my .htaccess file:
RewriteCond %{THE_REQUEST} /post/?([^\s&]+) [NC]
RewriteRule ^ /post/%1 [R=302,L,NE]
i am using php GET parameters, i am attempting for when the browser visits example.com/post/sometitle that the page that is currently example.com/post/?sometitle is displayed
In that case you need to the opposite of what you are asking in your question: you need to internally rewrite (not externally "redirect") the request from example.com/post/sometitle to example.com/post/?sometitle.
However, you must have already changed all the URLs in your application to use the new URL format (without the query string). You shouldn't be using .htaccess alone for this.
I also assume that /post is a physical directory and that you are really serving index.php in that directory (mod_dir is issuing an internal subrequest to this file). So, instead of /post/?sometitle, it's really /post/index.php?sometitle?
For example:
RewriteEngine On
# Rewrite /post/sometitle to filesystem path
RewriteRule ^post/([\w-]+)$ /post/index.php?$1 [L]
So, now when you request /post/sometitle the request is internally rewritten and handled by /post/index.php?sometitle instead.
I have assumed that "sometitle" can consist of 1 or more of the characters a-z, A-Z, 0-9, _ and -. Hence the regex [\w-]+.
If this is a new site then you can stop there. However, if you are changing an existing URL structure that has already been indexed by search engines and linked to by external third parties then you'll need to redirect the old URLs to the new. (Just to reiterate, you must have already changed the URL in your application, otherwise users will experience repeated redirects as they navigate your site.)
To implement the redirect, you can add something like the following before the above rewrite:
# Redirect any "stray" requests to the old URL
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ([\w-]+)
RewriteRule ^post/$ /post/%1 [R=302,NE,QSD,L]
The check against the REDIRECT_STATUS environment variable is to ensure we only redirect "direct requests" and thus avoiding a redirect loop.
(Change to 301 only when tested as OK, to avoid caching issues.)
In Summary:
RewriteEngine On
# Redirect any "stray" requests to the old URL
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ([\w-]+)
RewriteRule ^post/$ /post/%1 [R=302,NE,QSD,L]
# Rewrite /post/sometitle to filesystem path
RewriteRule ^post/([\w-]+)$ /post/index.php?$1 [L]
UPDATE: If you have multiple URLs ("folders") that all follow the same pattern, such as /post/<title>, /home/<title> and /build/<title> then you can modify the above to cater for all three, for example:
# Redirect any "stray" requests to the old URL
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ([\w-]+)
RewriteRule ^(post|home|build)/$ /$1/%1 [R=302,NE,QSD,L]
# Rewrite /post/sometitle to filesystem path
RewriteRule ^(post|home|build)/([\w-]+)$ /$1/index.php?$2 [L]
Aside: (With my Webmasters hat on...) This is not really much of an "improvement" to the URL structure. If this is an established website with many backlinks and good SE ranking then you should think twice about making this change as you could see a dip in rankings at least initially.
If only changing from query is your requirement then try with below, we are using QSD flag to discard our query string after our rule matched.
RewriteCond %{QUERY_STRING} ([^\s&]+) [NC]
RewriteRule ^ /post/%1 [R=302,L,NE,QSD]

Htaccess - Drop part of URL when condition matches

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

URL Rewrite subdomain with variables to regular domain with variables

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.

trouble with simple mod_rewrite redirect rule

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]

Apache / Linux - .htaccess how to get specific variable from a query / url and apply to rewrite

I have a rule that works for one "direction" but, not the other.
A typical incoming url / query would be: (long url)
http://somedomain.com/getme.pl?dothis=display&partnum=1234567 (could be up to 9 digits)
I have this rule in place in my htaccess file:
RewriteRule ^([0-9]*)$ /getme.pl?dothis=display&partnum=$1 [L]
Which works great for a bit of ease getting one of the unique part numbers:
http://somedomain.com/1234567.
However, I would like to make the long url "pretty" so, I assumed I could reverse(ish) it.
So, when a link on the site is clicked on (the long url) the htaccess file would process the long url to the beautified version.
I tried MANY attempts.
Here was my latest failure.
RewriteRule ^([0-9]*)$ /getme.pl?dothis=display&partnum=$1 [L] #(works)
RewriteCond %{QUERY_STRING} ^partnum=([0-9]*) #(tried to get partnum)
RewriteRule ^.* http://%{HTTP_HOST}/%1 [R] #(make the short url)
RewriteRule ^([0-9]*)$ /getme.pl?dothis=display&partnum=$1 [L] #(the known working rule)
I have tried a plethora of rules and visited many sites for advice.
I tried with just rules, just conditions and variations of query_string.
So, I believe I must just grab the "partnum" from the query and rewrite to /1234567 or http_host/1234567
Then, allow the other rule (works) to process.
So BOTH:
http://somedomain.com/getme.pl?dothis=display&partnum=1234567
and
http://somedomain.com/1234567
Display as: http://somedomain.com/1234567 in the browser.
and both passed the whole query to the getme.pl script properly.
I found some close answers here but, none that really explained what I needed.
Can someone please help?
From the sounds of it, this should get you moving down the right path:
# Your working rewrite, with extra param on the rewrite
RewriteRule ^([0-9]*)$ /getme.pl?dothis=display&partnum=$1&rewrite [L]
# Redirect for long urls, to pretty url
# -The appended '&rewrite' on the first rule will force this not to match
# -The trailing '?' on the rewrite will strip the query string
RewriteCond %{QUERY_STRING} partnum=([0-9]*)$
RewriteRule (.*) /%1? [L,R=301]
Hope that helps.

Resources