Rewrite a rewritten url beautified by .htaccess - .htaccess

I need to rewrite a rewritten url by the CMS in .htaccess.
The url I want to rewrite is:
http://whatever/foo/offer/?offer_id=1213/
To:
http://whatever/foo/offer/1213/
Tried a lot of things, but normal rewrite does not work, like:
RewriteRule ^offer/(.*)?$ offer/offer_id=$1 [L,NC]
RewriteRule ^offer/(.*)?$ offer/?offer_id=$1 [QSA]
etc.
How may I do this?
Should I edit the plugin or it can be done with .htaccess?

EDIT: WRONG!
You've got your rule backwards, and a little wrong. ^ means "start of URL" and $ means "end of URL". Try:
RewriteRule ^(.*)offer/\?offer_id=(.*)/ $1offer/$2
It's a bit clunky but should work.

Related

htaccess to remove part of a url, .htaccess

I was wondering how it is possible to remove part of a url with htaccess for example I have a url like this:
http://localhost/item.php?pa=gd34392
now I need to use htaccess to redirect this page or anything like this to a page with this url:
http://localhost/gd34392
I have tried things like this but nothing seems to work and I do not really know my way around htaccess
RewriteEngine on
RewriteRule item.php?pa=$1 /$1 [R=301,L]
You can achieve that using the following rules in your .htaccess:
RewriteEngine On
RewriteRule ^([^/]*)$ /item.php?pa=$1 [L]
Just make sure you clear your cache before testing this.

How do i URL rewrite using .htacess

i need some help rewriting URL using .htaccess my urls looks like:
http://www.example.com/testro2/product.php?id=426834&productStore=396
And i need have it work with text into it, example:
http://www.example.com/testro2/product/{anythinghere}/id=4268334&productStore=369
or you can make it look more clear like removing id= and &productStore= will be great, else i can use with them. example
http://www.example.com/testro2/product/{anythinghere}/4268334/396
The thing is that i need to have my keyword anythinghere in the URL.
Also, i would like to rewrite
http://www.example.com/testro2/search.php?q=shoes
TO
http://www.example.com/testro2/search/shoes
You can put this code in your /testro2/.htaccess
RewriteEngine On
RewriteBase /testro2/
RewriteRule ^product/.+/(\d+)/(\d+)$ product.php?id=$1&productStore=$2 [L]
RewriteRule ^search/(.+)$ search.php?q=$1 [L]

Why doesn't this URL rewrite work?

This is a page on my domain: www.mydomain.com/en/stats.php
I want it to look like this: www.mydomain.com/en/statistics/players
This is pretty simple to accomplish, but for some reason the htaccess code below doesn't work.
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^/statistics/players stats.php [R=301,L,QSA]
I get a 404 error when I try to open the SEO-friendly version. The page opens fine when I use the regular URL. What am I doing wrong?
The URL might not have a leading slash. Try without or optional slash. Additionally, you must check for the leading en, when you anchor your pattern at the beginning
RewriteRule ^/?en/statistics/players /en/stats.php
What worked in the end is the following:
RewriteRule en/statistics/players en/stats.php [NC,L]
I think it needs to be:
RewriteEngine On
RewriteRule ^en/statistics/players en/stats.php [QSA]
The R=301 means it will perform a redirect instead of a rewrite and try to redirect to /stats.php which doesnt exists I guess.
The L flag means that this is the last rewrite rule that will be processed for this request, which in this case I doubt is what you want. If there are rewrite rules further down for /en/stats.php they wont be processed.

htaccess clean URL's what's the best way to do it?

I'm working on clean Url's for my website and I noticed that what you find on the internet is pretty much about rewriting your clean urls to the url that your server can work with. So something like this:
www.domain.com/profile/username --> www.domain.com/profile.php?u=username
RewriteEngine On
RewriteRule ^profile/(.*)$ /profile.php?u=$1 [L]
So now with this I should link within my website to the cleanurl's. But intuitively it feels more solid/better to link to the 'dirty' url. So in that case you should use rewriterules for the above AND a redirect rule to rederict the 'dirty url' to the clean url, so the user only sees the clean url in the browser at all time.
www.domain.com/profile.php?u=username --> www.domain.com/profile/username
Is this something that is a good thing to do. Is it done by websites? What are arguments against/ in favour of it?
And how do you do you do a redirect like this. Something like the following doesn't seem to work:
RewriteRule ^profile\.php?u=(.*)$ /profile/$1 [R=301, L]
I'm fairly new to .htaccess so I might ask for something that's obvious...
RewriteRule ^profile\.php?u=(.*)$ /profile/$1 [R=301, L]
Isn't going to work because you can't match against the query string from a RewriteRule (you also have a stray space in your flags). You also want to be matching against the actual request and not the URI because you'd just cause a redirect loop from your other rule.
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /profile\.php\?u=([^&\ ]+)
RewriteRule ^/?profile\.php$ /profile/%1 [R=301,L]

.htaccess file using 2 different rewrite rules

I've got a .htaccess file that has got a rewrite rule in it as follows which works fine:
RewriteRule ^solicitorsin([^/]+)/all/([0-9]+)$ /search/searchresults.php?county=$1&page=$2 [L]
What I'm looking to do is to keep using this for if the page variable is 2 or higher, but if it's 1 I want to 301 redirect to a separate url (the same site) say http://www.domain.com/solicitorsinCOUNTY/
The problem is that if I try doing this using a 301 redirect or a rewrite rule it still performs the above rewrite rule as well so I end up with http://www.domain.com/solicitorsinCOUNTY/?county=COUNTY&page=1
I haven't done much with .htaccess before so I'm not even sure if this is possible, can anyone help please? It would be much appreciated.
If you are using a rewrite rule, then put the rule for page=1 above the other rule and make sure you have the [L] flag.
Alternatively, you can use RewriteCond to prevent the rule from being run on specific URLS like this:
RewriteCond %{REQUEST_URI} !^solicitorsin([^/]+)/all/1$
RewriteRule ^solicitorsin([^/]+)/all/([0-9]+)$ /search/searchresults.php?county=$1&page=$2 [L]

Resources