Hide particular parameter in URL along with value - .htaccess

I have URLs like
www.website.com/url?booking_id=1234
I need to hide the booking_id along with the value using htaccess.. So that, the URL will be like www.website.com/url
I've tried this rule, but it is not working
RewriteCond %{QUERY_STRING} .
RewriteRule ^([^.]*)$ /$1? [L,NE,R=301]

Related

Redirect to 'pretty-urls' doesn't work

I want to make my website to have 'pretty-urls'. Also I want to make so users which input 'ugly' urls will be redirected to 'pretty-urls'.
Simplified example:
I have page data.php?id=123. I want so it shows to users as data/123 and for whose who typed in address bar data.php?id=123 it will be redirected to data/123.
I have tried following code in .htaccess:
# redirect to pretty url
RewriteCond %{QUERY_STRING} ^id=([0-9]+)$
RewriteRule ^/?data\.php data/%1? [R=301,L]
# convert pretty url to normal
RewriteRule ^/?data/([0-9]+)$ data.php?id=$1 [L]
However it doesn't work, it goes into infinite loop as I understood.
Is what I wanted possible and how if yes?
Using %{QUERY_STRING} for your case will produce a infinite loop because the internal destination also relies on it.
However using %{THE_REQUEST} does not:
# Redirect /data.php?id=123 to /data/123
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+data\.php\?id=([0-9]+) [NC]
RewriteRule ^ /data/%1? [R=302,L]
# Internally forward /data/123 to /data.php?id=123
RewriteRule ^data/([0-9]+)/?$ /data.php?id=$1 [NC,L]

Redirect olddomain querystring layout to newdomain and new querystring layout?

OK, say my blog site myurl.org as many links to a separate domain:
old.myurl.org?oldvar=foo
Only old.myurl.org no longer exists and has been replaced by new.myurl.org.
If the query string vars were the same on new.myurl.org, I believe I could rewrite from .htaccess using:
RewirteCond %{http_host} ^old.myurl.org$ [NC]
RewriteRule ^(.*)$ new.myurl.org [L,R=301,QSA]
The only problem is that I also need to change the query string var from oldvar to newvar and preserve it's data (foo).
There are plenty of examples of how to rewrite query string vars in different ways, but I can't seem to find an example of this scenario.
I need to rewrite:
old.myurl.org?oldvar=foo
To:
new.myurl.org?newvar=foo
Edit
Furthermore, I have several potential query string key values to account for, but not all will always be present.
So I may need:
old.myurl.org?oldvar=foo&oldvar2=bar --> new.myurl.org?newvar=foo&newvar2=bar
or
old.myurl.org?oldvar2=bar --> new.myurl.org?newvar2=bar
This may not even be possible, but in one case I need to strip off part of the query string value. So ?oldvar=foo{term} might need to become ?newvar=foo
Thanks again!
First condition match the domain, second condition match the query string and the rule will match your domain root:
RewriteCond %{HTTP_HOST} ^old\.myurl\.org$ [NC]
RewriteCond %{QUERY_STRING} ^oldvar=(\w+)$
RewriteRule ^$ http://new.myurl.org/?newvar=%1 [R=301,L]
You can place more than one query string as well:
RewriteCond %{HTTP_HOST} ^old\.myurl\.org$ [NC]
RewriteCond %{QUERY_STRING} ^oldvar=(\w+)$ [OR]
RewriteCond %{QUERY_STRING} ^oldvar=(\w+)&oldvar2=(\w+)$
RewriteRule ^$ http://new.myurl.org/?newvar=%1&newvar2=%2 [R=301,L]
RewriteCond %{HTTP_HOST} ^old\.myurl\.org$ [NC]
RewriteCond %{QUERY_STRING} ^oldvar2=(\w+)$
RewriteRule ^$ http://new.myurl.org/?newvar2=%1 [R=301,L]
To use a path like x/y/z as you have mentioned on the comment you change the RewriteRule for example the rule we are currently using, will only redirect from yourdomain.com/?... which is:
RewriteRule ^$ http://new.myurl.org/?newvar2=%1 [R=301,L]
If you want to catch a different path you would do this:
RewriteRule ^x/?$ http://new.myurl.org/?newvar2=%1 [R=301,L]
The above would catch yourdomain.com/x?... and yourdomain.com/x/?...
You can have more than one path as well and use a OR condition like this:
RewriteRule ^(x|x/y|x/y/z)/?$ http://new.myurl.org/?newvar2=%1 [R=301,L]
The above means we want to match x/?... OR x?... OR x/y?... and x/y/?... OR x/y/z?... OR x/y/z/?...
By encapsulating it on the parenthesis and using the | as separator and OR.
The ^ and $ means match from begin to end, so when it contains nothing means match nothing which means root folder domain.com/, when there is content it will match the content for instance domain.com/x or whatever you place into it.

301 redirect get pagination

I'm trying to 301 redirect a paginated blog list from an old site onto a new url.
I think I'm getting pretty close with the RewriteRule but I'm not quite there yet, this is what I have:
RewriteCond %{QUERY_STRING} ^page=
RewriteRule ^(blog)?$ http://www.newdomain.com/news/page/$1? [R=301,L]
Using this rule if I go to
http://www.olddomain.com/blog?page=1
I currently get redirected to
http://www.newdomain.com/news/page/blog
I would like to be sent to
http://www.newdomain.com/news/page/1
I'm sure its just something small and simple that I'm missing.
Edit
Expanding on the solution below, I've added tags/category support to the rewrite rule using $1.
RewriteCond %{QUERY_STRING} ^page=([^&]+) [NC]
RewriteRule ^blog/tag/([^/\.]+)?$ http://www.newdomain.com/news/tag/$1/page/%1? [R=301,L,NC]
Few minor mistakes in your code.
You need to capture page parameter's value from query string first
Then use that capture value using % instead of $1
No need to capture blog since you don't need it.
Change your code with:
RewriteCond %{QUERY_STRING} ^page=([^&]+) [NC]
RewriteRule ^blog/?$ http://www.newdomain.com/news/page/%1? [R=301,L,NC]

How do I redirect a url with parameters

How do I redirect a url with parameters
I will like to write this url:
http://mydomain.com/dk/silkeborg?latitude=56.1631229&longitude=9.536976500000037&zoom=14
and redirect it to
http://mydomain.com/index.php?country=dk&city=silkeborg&latitude=56.1631229&longitude=9.536976500000037&zoom=14
To day I use the below rule, that don't take any parameters
RewriteRule (dk|de)/(.*) index.php?country=$1&city=$2 [NC]
Please help me
You do need to use [QSA] because you're rewriting the query (it's enabled by default unless you rewrite the query). Here we're capturing the country code and passing it as a var. Then QSA is catching the rest of the variables and adding them to the query string.
RewriteCond %{REQUEST_URI} ^/(dk|de)/ [NC]
RewriteRule .* index.php?country=%1 [QSA,L]
To use your existing rule, you would append [QSA]
RewriteRule (dk|de)/(.*) index.php?country=$1&city=$2 [QSA,NC,L]
When the page loads you can access the country via $_GET['country'] and city via $_GET['city'].

Why is my pagination not working after mod rewrite?

Before rewriting my url file.php?style=foo&page=2 works fine.
After rewrite it stops working and just stays on the same page with the ?page variable at the end like it's not getting caught:
foo-new?page=2
After the rewrite the page parameter stops working on the clean url.
Here's what my rewrite rules look like:
RewriteCond %{QUERY_STRING} ^style=([A-Za-z-]+)/?$
RewriteRule ^file\.php$ %1-new? [NS,R=301,L]
RewriteRule ^([A-Za-z-]+)-new/?$ file.php?style=$1&redirect=no [NS]
So I translate what you'd like to do then, I change your rules and write them. Please tell me if I'm right or wrong.
You'd like to check if in the query string you have an optional parameter "style".
RewriteCond %{QUERY_STRING} ^style=([A-Za-z-]+)/?$
What is the point of the ^ and the /? and the $ ? Remove them:
RewriteCond %{QUERY_STRING} style=([A-Za-z-]+)
It's clearer this way and should still work for what you want.
RewriteRule ^file\.php$ %1-new? [NS,R=301,L]
What is the point of the ? ? Remove it:
RewriteRule ^file\.php$ %1-new [NS,R=301,L]
So, please try these rules and tell me if they work, and if not, please be more specific or give real URL sample that you'd like to rewrite:
RewriteCond %{QUERY_STRING} style=([A-Za-z-]+)$
RewriteRule ^file\.php$ %1-new [QSA,NS,R=301,L]
RewriteRule ^([A-Za-z-]+)-new/$ file.php?style=$1&redirect=no [NS]

Resources