I have a special use case where I need to redirect URL's starting with a few keywords to the base URL.
So for example I want to redirect:
/test1 to /?utm_campaign=test1
and
/test2 to /?utm_campaign=test2
This works fine using this rule:
RewriteEngine On
RewriteBase /
RewriteRule ^(test1|test2) ?utm_campaign=$1 [R=301,L]
But I also want to preserve other query string parameters, so:
/test1?utm_content=abc should redirect to /?utm_campaign=test1&utm_content=abc
I've tried it with QSA, which works in Chrome, but not Safari:
RewriteEngine On
RewriteBase /
RewriteRule ^(test1|test2) ?utm_campaign=$1 [R=301,L,QSA]
Chrome redirects it correctly, but Safari just redirects to /?utm_campaign=test1
I've also tried adding the full query string to the target URL, like so:
/?utm_campaign=test1&q=
This also works in Chrome, but not safari, using this rule:
RewriteEngine On
RewriteBase /
RewriteRule ^(test1|test2) ?utm_campaign=$1&q=%{QUERY_STRING} [R=301,L]
Any ideas how to append my query string parameters to the target URL?
Edit: The problem turned out not to be with the rewrite rules, but with caching of the url parameters on the hosting side. I've had the hosting company add some caching exclusions and after that everything works as expected.
Although your second rules are correct ,I will give you another solution without using [QSA] flag :
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^(.+|.*)$
RewriteRule ^(test1|test2) ?utm_campaign=$1&%1 [R=302,L]
Clear browser cache then test it , if it is Ok , replace 302 with 301.
With rules above server will redirect any URI starts with test1 or test2 and check if there is a query string or not then append it after that redirection target.
Related
I am trying to redirect my site using .htaccess if the url contains a certain parameter.
For example when my site contains ?lang=de, then I would like to have the site to be permanently redirected to https://www.example.com/de/
This is what I've tried:
# BEGIN Redirects for mobile params
RewriteEngine On
RewriteCond %{REQUEST_URI} lang=de
RewriteRule .* https://www.example.com/de/
# END Redirects for mobile params
Any help would be appreciated, thanks
UPDATE: This is what I'm trying now, but I'm getting:
# BEGIN Redirects for mobile params
RewriteEngine on
# if the query string contains a parameter called "lang"
RewriteCond %{QUERY_STRING} (?:^|&)lang=de
# then redirect (permanently) to /
RewriteRule ^ https://www.example.com/de/ [L,R=permanent]
# END Redirects for mobile params
But this is resulting in ERR_TOO_MANY_REDIRECTS
UPDATE: I found that when it put this at the end of my .htaccess that it starts to work.
I found that when it put this at the end of my .htaccess that it starts to work.
This may be due to earlier directives that prevent the redirect loop (in other words there is a conflict). Ideally, this redirect should be near the start of the .htaccess file.
However, you should still discard the query string from the redirected response. As it stands, your redirect to /de/ will still contain the lang=de URL parameter. The QSD flag on Apache 2.4 will do this. For example:
RewriteCond %{QUERY_STRING} (?:^|&)lang=(de)
RewriteRule !de/ /%1/ [QSD,R=permanent,L]
The %1 backreference contains the "de" captured in the query string.
I am attempting to redirect a development site that has been indexed by search engines to a specific category in the new live site.
The development site URL was:
http://staging.mydomain.com.au/
There are multiple page url's after the domain so I'd like to redirect all of these to the same new URL.
E.g.
http://staging.mydomain.com.au/essential_grid/
http://staging.mydomain.com.au/feed/
The new url is, where "/portfolio/" is where I need the old URLs redirected to:
http:www.mydomain.com.au/portfolio/
While I'm not receiving any error messages, staging.mydomain.com.au is
not redirecting to www.mydomain.com.au/portfolio/
This is a snippet of what I have in my .htaccess file at present:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^staging.mydomain.com.au$
RewriteRule ^(.*)$ http://www.mydomain.com.au/portfolio [R=301,L]
</IfModule>
Any help would be greatly appreciated.
UPDATE: It seems (from comments) that staging. no longer exists - that's the problem! staging. needs to exist (ie. the DNS must resolve) so that the request gets to your server in order to do the redirect. If staging. does not exist then the initial request will simply fail (the browser will fail to lookup the domain and you'll get a DNS error).
You need to recreate the staging subdomain (even with no files) and then implement the redirect below.
Your RewriteRule actually looks OK - assuming you are wanting to redirect all those URLs to the single /portfolio URL. However, the order should perhaps be changed to have the redirect before the internal rewrite. RewriteEngine only needs to be included once and you don't need RewriteBase in the code you have posted. So, this should be rewritten as:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^staging\.mydomain\.com\.au$
RewriteRule ^ http://www.mydomain.com.au/portfolio [R=301,L]
^(.*)$ is reduced to simply ^ since you don't need to capture the URL for a backreference in the substitution.
I've also removed the other RewriteRule as it doesn't appear to be doing anything?
Also note that 301 redirects are cached by the browser, so make sure that you clear your browser cache between failed attempts. It can be easier to test with 302 temporary redirects for this reason.
Excuse me for my english.
I make a brands directory web site.
Before to acces to the brands pages I use requests like this :
mydomain.com/fiche.php?id=115
where id is the id of the brand in my directory
I change the structure of the brands pages and now use this request:
mydomain.com/annuaire.php?type=fiche&id_marq=115
where id has become id_marq
I try to use a rewritebrule like this:
RewriteRule ^fiche.php$ http://www.annuaire-sites-officiels.com/annuaire.php?detail=fiche&id_marq=$1 [L,QSA,R=301]
to redirect the old links to the new pages but result dont pass the id_marq value and the url is:
http://www.annuaire-sites-officiels.com/annuaire.php?detail=fiche&id_marq=&id=115
&id= is too.
What am I doing wrong?
Your rule is not evaluating query string and that's why its not capturing id query parameter.
Change your code to:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^id=([^&]+) [NC]
RewriteRule ^fiche\.php$ /annuaire.php?detail=fiche&id_marq=%1 [R=302,L,QSA,NC]
Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.
Check out Regex Back Reference Availability:
You have to capture the query string. [QSA] passes it forward unaltered, so unless you're using id for anything you don't need that bit of code. Your 301 redirect is correct since this is a permanent redirect. Remember if you add a failed redirect your browser may cache that redirect so it might not look like it's working.
In this string match I'm only catching numbers to prevent someone from passing something like an asterisk * and XSS exploiting your site.
I've not included and [NC] matches in my code because when you allow multiple cases they can seem like different URLs to search engines (bad for SEO).
RewriteCond %{QUERY_STRING} id=([0-9]+)
RewriteRule ^fiche.php$ http://%{HTTP_HOST}/annuaire.php?detail=fiche&id_marq=%1 [R=301,L]
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]
I have a news (blog) site that returns urls in the following format when individual posts are selected:
website.net/sitenews.php?q=posts/view/postname/12
I am seeking to rewrite the url so that it reads:
website.net/sitenews.php/posts/view/postname/12
or any other way where the ?q= is removed for purpose of removing the ? so that the url can be accessed by facebook's like button as the facebook url linter does not parse query strings.
In the htdocs .htaccess file in the root directory I have tried the following:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{QUERY_STRING} q=
RewriteRule (.*) website.net/sitenews.php/$1? [R=301]
This successfully removes the q=? however the rest of the string (posts/view/postname/12) is not returned and the url now looks as follows:
website.net/sitenews.php/sitenews.php
Does anyone have any suggestions to help me complete this url_rewrite?
Try this instead in your .htaccess:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{QUERY_STRING} ^q=(.*)$ [NC]
RewriteRule ^(.*)$ /$1/%1? [R=301,L,NE]
R=301 will redirect with https status 301
L will make last rule
NE is for no escaping query string
%1 is capture group for query string q= (whatever comes after q=)
$1 is your REQUEST_URI
If you are using any CMS, like wordpress, or joomla or SE, then you have option to do that else you need to have an .htaccess file where you can write the code, refer this links
http://roshanbh.com.np/2008/03/url-rewriting-examples-htaccess.html
http://www.webmasterworld.com/forum92/2545.htm
http://www.google.com/#sclient=psy&hl=en&q=htaccess+change+the+url&aq=0p&aqi=p-p1g4&aql=&oq=htaccess+&pbx=1&bav=on.2,or.r_gc.r_pw.&fp=c875dd2b8adea15a