rewrite url with query string issue - .htaccess

i have a issue with some rewriting url with htaccess.
i need the following:
old: mydomain.com/categorie/details.php?companyid=10
new: mydomain.com/item/?p=10
where i want to catch and replace the numbers (of several id) dynamically.
I've read the answer of the other question but that's only replace the query string. I quess i need some regex but i'm a newbee with that..
i have this in htaccess:
RewriteCond %{REQUEST_URI} ^/categorie/details\.php$
RewriteCond %{QUERY_STRING} ^companyID=([0-9]*)$
RewriteRule ^(.*)$ www.mydomain.com/item/?p=%1/ [R=301,L]
Before i migrate i've tested this and it worked fine, for some reasons it won't work anymore after migration.
Do i miss something? Is this the wrong approach?
Kind regards,
Tom.

At this line
RewriteRule ^(.*)$ www.mydomain.com/item/?p=%1/ [R=301,L]
Put the ful URL like this :
RewriteRule ^(.*)$ http://www.example.com/item/?p=%1/ [R=301,L]
Also put this [NC] ,nocase to accept upper-case and lower-case, at the end of this line
RewriteCond %{QUERY_STRING} ^companyID=([0-9]*)$ [NC].
So , your rules should look like this :
RewriteCond %{REQUEST_URI} ^/categorie/details\.php$
RewriteCond %{QUERY_STRING} ^companyID=([0-9]*)$ [NC]
RewriteRule ^(.*)$ http://www.example.com/item/?p=%1 [R=301,L]
To sumerize all these rules more , do this :
RewriteCond %{QUERY_STRING} ^companyID=([0-9]*)$ [NC]
RewriteRule ^categorie/details\.php$ http://www.example.com/item/?p=%1 [R=301,L]
And as you menthioned that it is same domain , let them be like this :
RewriteCond %{QUERY_STRING} ^companyID=([0-9]*)$ [NC]
RewriteRule ^categorie/details\.php$ /item/?p=%1 [R=301,L]
No need to full URL.
Note: clear browser cache then test

Related

How do I ignore the end of a url parameter string in Htaccess?

I have a bunch of ugly links coming in to a site like this:
https://www.somedomain.com/mm5/mch.mvc?Session_ID=5f59c6e0&Screen=PRODFB&Product_Code=pcode1&Category_Code=category_a&Store_Code=store1&fb=1
I'm trying to use htaccess to recreate the url like this:
https://www.somedomain.com/mm5/mch.mvc?Screen=PROD&Product_Code=pcode1
I've come up with this but of course it isn't working. I think I just need to be able to ignore the rest of the url parameter after the product_code param but not sure how to do it.
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^Screen=PRODFB&Product_Code=([^/.]+)$ /mm5/mch.mvc?Screen=PROD&Product_Code=$1 [R=301,L,NE]
What am I missing? Thanks!
You cannot match query string in RewriteRule directive.
As long as query parameters are same as what you have in question, you may use this rule in your site root .htaccess:
RewriteEngine On
RewriteCond %{THE_REQUEST} /(mm5/mch\.mvc)\?.*&Screen=PRODFB&(Product_Code=[^\s&]+) [NC]
RewriteRule ^ /%1?Screen=PROD&%2 [R=301,L,NE]
Or using %{QUERY_STRING}:
RewriteCond %{QUERY_STRING} (?:^|&)Screen=PRODFB&(Product_Code=[^&]+) [NC]
RewriteRule ^mm5/mch\.mvc/?$ %{REQUEST_URI}?Screen=PROD&%1 [R=301,L,NE]
I was able to get it work like this:
RewriteCond %{QUERY_STRING} Screen=PRODFB&Product_Code=([^&]+)
RewriteRule ^(.*)$ /mm5/mch.mvc?Screen=PROD&Product_Code=%1& [R=301,L,NE]

Remove parenthesis from htaccess (Helicon Ape)

I'm using Helicon Ape on a Windows Server to create htaccess files.
Originally, part of a larger set of conditions, I had this condition set to return 403 if the url contained (). However it is causing false positives in case of mailchimp tracking codes that end up getting wrapped in ()
RewriteCond %{QUERY_STRING} ^.*(\[|\]|\(|\)|<|>).* [NC,OR]
For example in the below URL
http://domain.com/page/11/page-name?ct=t(Newsletter_Tracking)
As an alternative, I was attempting to remove the parenthesis and redirect to a "cleaned version".
I tried a few different things that I found in SO but none worked.
So far the closest thing that I could get to working is this:
RewriteCond %{REQUEST_URI} [\(\)]+ [OR]
RewriteCond %{QUERY_STRING} [\(\)]+
RewriteRule ^(.*)[\(]+([^\)]*)[\)]+(.*)$ $1$2$3 [R=301,L]
The problem with the above code is that works if the () were in the URL but not the query string. It doesn't redirect and clean the querystring.
So this would work:
http://domain.com/page/11/pag(e-name)
but this wouldn't:
http://domain.com/page/11/page-name?ct=t(Newsletter_Tracking)
Your assistance is appreciated
Thank You.
You can use the following rule :
RewriteCond %{THE_REQUEST} /page/11/page-name\?ct=t\(Newsletter_Tracking\)\sHTTP [NC]
RewriteRule ^ %{REQUEST_URI}? [L,R]
If the querystring is dynamic, try:
RewriteCond %{THE_REQUEST} /page/11/page-name\?ct=.+\(.+\)\sHTTP [NC]
RewriteRule ^ %{REQUEST_URI}? [L,R]
Using #starkeen 's example, I was able to create a working solution.
This code handles the Query String separate from the URL. It cleans the URL but removes the query string.
RewriteCond %{REQUEST_URI} [\(\)]+
RewriteRule ^(.*)[\(]+([^\)]*)[\)]+(.*)$ $1$2$3 [R=301,L]
RewriteCond %{QUERY_STRING} [\(\)]+
RewriteRule ^ %{REQUEST_URI}? [R=301,L]

Remove query string - rewrite rule

How do i remove a query string from the end of a url?
To be more specific, this is my rewrite rule:
RewriteRule example-(.*).html$ examples/view-example.php?param1=parameter&param2=$1&split=-
and I want this to return a 404 or a redirect to www.mydomain.com/example-one.html :
www.mydomain.com/example-one.html?param1=parameter&param2=one&split=-
This is what i tried, it doesn't work:
RewriteCond %{REQUEST_URI} /examples/view-example\.php
RewriteCond %{QUERY_STRING} param1=parameter&param2=(.*)&split=-
RewriteRule ^(.*)$ http://mydomain.com/example-%1.html$
I think that RewriteRule ^(.*)$ http://mydomain.com/example-%1.html$ isn't correct..
This should do the trick:
RewriteCond %{QUERY_STRING} ^param1=parameter&param2=(.*)&split=-
RewriteRule ^/examples/view-example\.php$ http://mydomain.com/example-%1.html [R=301]
Though, I didn't understand what you wanted to do with www.mydomain.com/example-one.html?param1=parameter&param2=one&split=-

.htaccess, Rewriterule not working as i want

I have this link: http://www.domain.com.mk/lajmi.php?id=2790,
and i want to change it to http://www.domain.com.mk/lajmi/2790
With this code I can change the link to /lajmi/2790 but i get 404 error.
I mean i get the link
http://www.domain.com.mk/lajmi/2790, but it has 404 error (i dont se the content)
This is my code:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.com\.mk$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.com\.mk$
RewriteCond %{QUERY_STRING} ^id=([0-9]*)$
RewriteRule ^lajmi\.php$ http://domain.com.mk/lajmi/%1? [R=302,L]
What I am doing wrong ?
Try this one :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com\.mk$
RewriteCond %{QUERY_STRING} ^id=(\d*)$
RewriteRule ^lajmi\.php$ http://domain.com.mk/lajmi/%1? [R=302,L]
RewriteRule ^lajmi/(\d*)$ lajmi.php?id=$1&r=0 [L]
(the &r=0 in the final rule is for not getting an infinite loop)
Single direction rewrite:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com\.mk$
RewriteRule ^lajmi/(\d*)$ lajmi.php?id=$1 [L,QSA]
This means that every uri of kind /lajmi/2790 will be passed to /lajmi.php?id=2790 in a sub-request.
However, in this case, if the user hits /lajmi.php?id=2790 by himself, then this is the url he will see in the browser, not the "beautified one".
Bi-directional rewrite:
RewriteEngine On
RewriteBase /
; Redirect lajmi.php?id=2790 to a beutified version, but only if not in sub-request!
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com\.mk$
RewriteCond %{IS_SUBREQ} !=true
RewriteCond %{QUERY_STRING} ^id=(\d*)$
RewriteRule ^lajmi\.php$ lajmi/%1 [R=301,L]
; Make the beutified uri be actually served by lajmi.php
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com\.mk$
RewriteRule ^lajmi/(\d*)$ lajmi.php?id=$1 [L]
Here, an additional RewriteCond was added to the first rule checking that this is not a sub-request, to ensure that the rules do not loop.
You can pick which way you like, but the first approach is enough if you build the links in your HTML in the 'beautified' way already (no need to redirect the browser twice just to see the page).

Help with .htaccess RewriteRule. Need to take user from one URL to other

I want users who type
http://www.example.com/word-of-the-day
to be taken to
http://www.example.com/index.php?page=word-of-the-day
But I want
http://www.example.com/word-of-the-day
to be shown in the URL for the user.
What should I do in my .htaccess? I tried but the regular expression
and the syntax of RewriteRule is way too complicated for me to
figure out how to do it.
Any help will be appreciated.
Edit:
Also, how can I say this in htaccess -
if they type http://www.example.com/word-of-the-day, take them to http://www.example.com/index.php?page=word-of-the-day
or if they type http://www.example.com/something-else, take them to http://www.example.com/index.php?page=something-else
or else, just take them to the URL they typed.
The condition below checks that index.php is not being requested. If not apply the rule. This will work for any of the scenarios you listed above.
RewriteCond %{QUERY_STRING} ^!.*[index\.php].*$ [NC]
RewriteRule ^(.*)$ index.php?page=$1 [L]
In response to your comment about only wanting to do this for a few specific pages, it would look like this(as an alternative to Nils edit):
RewriteCond %{QUERY_STRING} ^!.*[index\.php].*$ [NC]
RewriteCond %{REQUEST_URI} ^word-of-the-day$ [OR]
RewriteCond %{REQUEST_URI} ^something-else$ [OR]
RewriteCond %{REQUEST_URI} ^even-something-else$
RewriteRule ^(.*)$ index.php?page=$1 [L]
Try this
RewriteEngine on
RewriteRule ^word-of-the-day$ index.php?page=word-of-the-day
Or more flexible
RewriteEngine on
RewriteRule ^(.*)$ index.php?page=$1
Not tested, yet it sould work.
To your edit:
Just define those specific URLs manually:
RewriteEngine on
RewriteRule ^word-of-the-day$ index.php?page=word-of-the-day
RewriteRule ^word-some-example$ index.php?page=some-example
RewriteRule ^some-other$ index.php?page=some-other

Resources