I want to change my url from
http://intervideo.2watt.net/component/content/article.html?id=65&Itemid=479
to
http://intervideo.2watt.net/component/content/article.html?id=65&Itemid=332
I have tried the following code but it is not working
RewriteRule ^/?Itemid=479/(.*)$ Itemid=332/$1 [R=301,L]
You cannot capture query string from RewriteRule. Use a RewriteCond instead:
RewriteCond %{QUERY_STRING} ^(id=\d+)&Itemid=479$ [NC]
RewriteRule ^component/content/article\.html$ %{REQUEST_URI}?%1&Itemid=332 [L,NC,R=302]
Related
I need to handle one query parameter specifically.
The url can look like the following:
https://example.com/?app=egov&map=1337
https://example.com/?app=egov&map=1337&showoptions=true
https://example.com/?app=egov&map=1337&showoptions=true&anotherparam=helloWorld
The redirect should point to
https://example.com/contextName/resources/apps/egov/index.html?map=1337
https://example.com/contextName/resources/apps/egov/index.html?map=1337&showoptions=true
https://example.com/contextName/resources/apps/egov/index.html?map=1337&anotherparam=helloWorld
As you can see, the app-Parameter needs to be used as an URL-part; the others need to be used like traditional query-params.
I've figured out, how to implement the first part, where https://example.com/?app=egov points to https://example.com/contextName/resources/apps/egov/index.html using the following
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{QUERY_STRING} ^app\=(.+)
RewriteRule ^/$ "/contextName/resources/apps/%1/index.html" [R,L,QSA]
but I'm struggeling how to realize the correct handling of the other params.
All these combination of query string can be handled by a single rule loke this:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^app=([\w-]+)(?:&(.*))?$ [NC]
RewriteRule ^$ contextName/resources/apps/%1/index.html?%2 [L]
RewriteCond matches app query parameter and captures it in %1 while rest of the query string after & is captured in %2.
With your shown samples, please try following htaccess Rules file. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
####Apache documentation link https://httpd.apache.org/docs/trunk/rewrite/remapping.html for more info.
##Internal rewrite rule for URL https://example.com/?app=egov&map=1337
RewriteCond %{QUERY_STRING} ^app=([^&]*)&map=(\d+)$ [NC]
RewriteRule ^/?$ contextName/resources/apps/%1/index.html?map=%2 [L]
##Internal rewrite rule for URL https://example.com/?app=egov&map=1337&showoptions=true
RewriteCond %{QUERY_STRING} ^app=([^&]*)&map=(\d+)&showoptions=(.*)$ [NC]
RewriteRule ^/?$ contextName/resources/apps/%1/index.html?map=%2&showoptions=%3 [L]
##Internal rewrite rule for https://example.com/?app=egov&map=1337&showoptions=true&anotherparam=helloWorl
RewriteCond %{QUERY_STRING} ^app=([^&]*)&map=(\d+)&showoptions=([^&]*)&anotherparam=(.*)$ [NC]
RewriteRule ^/?$ contextName/resources/apps/%1/index.html?map=%2&anotherparam=%4 [L]
All of your URLs uri part is NULL, so rules are written based on that.
I have a redirect problem where I would like to redirect a url with query parameters to another PDF entirely, e.g.:
Original URL:
https://www.foo.bar.com/~/bar.pdf?la
Redirect URL:
https://www.bar.foo.com/foo.pdf
Currently my rewrite rule is as shown below:
RewriteRule ^/~/bar.pdf?la https://www.bar.foo.com/foo.pdf [R=301,L,NC]
How can I write a 301 redirect that both accounts for the tilde and the query parameter?
Thanks in advance for your help!
First your rule RewriteRule ^/~/bar.pdf?la https://www.bar.foo.com/foo.pdf [R=301,L,NC] has several issues .
First , RewriteRule ^/ it is not applicable here so RewriteRule ^ is enough.
Second , you should escape ~ like this RewriteRule ^\~
and finally , dont't write query string with RewriteRule like bar.pdf?la because it is not a pert of URI.
The line should look like this :
RewriteRule ^\~/bar.pdf https://www.bar.foo.com/foo.pdf [R=301,L,NC]
But the query string may append to new target , to prevent that add ? at the end of target this :
RewriteRule ^\~/bar.pdf https://www.bar.foo.com/foo.pdf? [R=301,L,NC]
If you want to use query string value , you may add condition before that to utilize it:
RewriteCond %{QUERY_STRING} ^(.*)$
you could change ^(.*)$ to whatever you want and you could use it in target like this example :
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)$ https://www.whatever.com/%1/$1 [R=301,L,NC]
So , %1 refer to this condition RewriteCond %{QUERY_STRING} ^(.*)$ and $1 refer to this condition RewriteRule ^(.*)$ and so on.
You need to use RewriteCond
directive to check the url query string. You can not do that in RewriteRule . The querystring in your url is la .
RewriteEngine on
RewriteCond ℅{QUERY_STRING} ^la$ [NC]
RewriteRule ^.*bar\.pdf$ /foo.pdf? [L,R=301]
In my .htaccess I'm trying to redirect the following page:
/category/item?id=81
To the following URL:
/category
This is where I got to far (it's at the top of my mod rewrites):
RewriteCond %{REQUEST_URI} ^/category/item$
RewriteCond %{QUERY_STRING} ^id=81$
RewriteRule ^(.*)$ http://mywebsite/category? [L,R=301]
However it's not redirecting the page. Just wondering if there's anything I need to add to this to get it to work?
Try it like this:
RewriteCond %{QUERY_STRING} ^id=81$ [NC]
RewriteRule ^category/item$ /category? [R=301,NE,NC,L]
or
RewriteCond %{QUERY_STRING} ^id=81$ [NC]
RewriteRule ^category/item$ http://mywebsite//category? [R=301,NE,NC,L]
Use a fresh browser in incognito mode to test it.
This did the trick for me (I solved it before the answers above were posted):
RewriteCond %{REQUEST_URI} ^/category/item$
RewriteCond %{QUERY_STRING} ^id=81
RewriteRule ^(.*)$ category? [L,R=301]
I need a hand with creating a regex rewrite rule for my .htaccess.
Here's the problem. My previous URLs structure was:
http://example.com/whatever-the-url-is/?lang=en
now I turned it into
http://example.com/en/whatever-the-url-is/
I'd like to create an .htaccess URL that 301 redirects all the URLs from the previous structure to the new one.
Also the .htaccess is shared between different domains, and so there should be
RewriteCond %{http_host} !^example.com$ [NC] condition at the beginning...
Is it possible? Do you have any suggestions?
You can use:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^example.com$ [NC]
RewriteCond %{QUERY_STRING} (?:^|&)lang=(.+?)(?:&|$) [NC]
RewriteRule ^(.*)$ /%1/$1? [R=301,L]
You can add the RewriteRule after RewriteCond %{HTTP_HOST}:
RewriteCond %{REQUEST_URI} !^/(?:en|fr|sp)/ [NC]
Where you test if langcode is already in the url.
I used to have a query string for handling mobile pages. Now the site is responsive it doesn't need it and I need to do 301 redirects let google know.
so
http://www.example.com/aaa/bbb?fromMobile=true
should become
http://www.example.com/aaa/bbb
and
http://www.example.com/aaa?fromMobile=true
should become
http://www.example.com/bbb
and
http://www.example.com?fromMobile=true
should become
http://www.example.com
etc
I have:
RewriteCond %{QUERY_STRING} ^(?)fromMobile=true$ [NC]
RewriteRule (.*) /$1?%1&%2 [R=301,L]
But this redirects:
http://www.example.com?fromMobile=true
to
http://www.example.com?
How can I get rid of the trailing question mark? I've tried a bunch of things without joy.
You can use:
RewriteCond %{QUERY_STRING} ^fromMobile=true$ [NC]
RewriteRule (.*) /$1? [R=301,L]
This is assuming there is no other query parameter besides fromMobile=true.
If there is a chance of other query parameters also then use:
RewriteCond %{QUERY_STRING} ^(.+?&)?fromMobile=true(?:&(.*))?$ [NC]
RewriteRule (.*) /$1?%1%2 [R=301,L]