Removing the Query String - .htaccess

I've been trying different methods for a couple of hours with no joy.
I have 20 or so urls like this
http://www.domain.com/news.php?id=77
http://www.domain.com/news.php?id=76
http://www.domain.com/news.php?id=73
and so on.
They can all go to
http://www.domain.com/news.php
Tried this...
RewriteRule ^news.php http://www.domain.com/news.php? [R=301,L]
...but doesn't work.
Thanks

The rule you have is pretty close. You need to check that you actually have a query string.
Try:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^id=
RewriteRule ^news\.php$ /news.php? [L,R=301]

I would try:
RewriteEngine On
RewriteRule ^news.php\?id=$ news.php [R=301,L]
And recommend this tutorial http://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/. It has a nice learn by example approach.

Related

rewrite all pages /en/XXX to /en

It's been a while since I've been busting my head to do a htaccess rewrite.
I would like to redirect all the pages from example.com/en/XXX to example.com/en.
I'm doing either redirection loops or errors 500.
Is it possible to help me to find the right formula?
I tried RewriteRule ^en/(.+)$ /en/ L,QSA
also this RewriteRule ^/en\/.*$ http://example.com/en/$1 [R=permanent,L]
Do you also know good links to learn htaccess and rewrite?
thank you in advance for your help
You need to put a condition to stop Looping, could you please try following once, base on your shown samples only.
RewriteEngine ON
RewriteCond %{REQUEST_URI} ^/en/.*/?$ [NC] [L]
RewriteRule ^(en)/.*$ http://example.com/$1 [NC,L]
Merci RaVinder pour ton aide ! <3

.htaccess rewrite rule to change path to query string? [duplicate]

Ok, im pretty new at this and I would really appreciate some help, thanks!
How can i rewrite this in .htaccess correctly?
So I have a query string in my url:
/?url=contact
All i want to do is remove the query string
/contact
Help? I scoured google and I'm learning the syntax right now, but the fact remains..I dont know how to do it just yet. Thanks to all
This was my solution:
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]
Try this:
RewriteEngine On
RewriteRule ^(.*)$ /index.php?url=$1 [L]
To a user on your site, they will see and navigate to this:
http://example.com/contact
But the real page would be something like this:
http://example.com/index.php?url=contact
This bit, [L], tells the server that this is the last line of the rewrite rule and to stop.
RewriteCond %{QUERY_STRING} url=(.*)
RewriteRule index.html %1
(or whatever if it's not index.html, index.php, whatever)
You need to capture the query string, which is not looked at by RewriteRule normally, and use the %1 back reference, not $1 as you would in a capture in a RewriteRule
Before: https://example.com/index.php?user=robert
RewriteEngine On
RewriteRule ^user/([^/]+)?$ index.php?user=$1 [L,QSA]
After: https://example.com/user/robert

Question mark in htaccess

I'm trying to make search results url look like Google.
site.com/search?q=blabla
Tried this
RewriteRule ^search\?q=(.*)$ index.php?q=$1
I used \ just before ? but it still doesn't work, the question mark makes the problem. What is the correct way to do that?
You just need this simple rule in root .htaccess:
RewriteEngine On
RewriteBase /
RewriteRule ^search/?$ index.php [L,QSA,NC]
Query string ?q=foobar will be automatically carried over to index.php.
Try this instead, this works for me to replace:
example.com/search.php?q=hello
With:
example.com/search/hello/
Code is:
RewriteCond /%{REQUEST_FILENAME}.php -f
RewriteRule ^search/?q=([a-zA-Z0-9-]+)$ /$1.php?q=$2`

.htaccess change /es/ to .es

I am working with a domain like:
http://www.domain.com/es/blabla.html
And I want to change the /es part for .es and convert the URLS to something like:
http://www.domain.com.es/blabla.html
I've been trying lots of possibilities but I haven't reached a solution.
For example one of them has been:
RewriteRule ^es/(.*)$ http://www.domain.com.es/$1 [L]
But it has entered in an infinite loop. Which I've tried to avoid adding this lines:
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
But the server doesn't allow this calls. So I still have the problem.
Check this.
RewriteEngine On
RewriteRule ^\.html$ /es/blabla.html [L]

mod_rewrite remove query string on top of a rewrite rule

I have the following rewrite rule:
RewriteRule ^(.*)-task-(.*)\.html$ /index.php/task/name/$2\-task\-$1 [L]
When I tried to open:
/heru-task-number-1.html
It is working fine. HOwever, when there is a query string appended to it:
/heru-task-number-1.html?whatever=value
It is actually not calling the correct rewrite. Thus, I wonder how can I make sure so that both:
/heru-task-number-1.html
AND
/heru-task-number-1.html?whatever=value
are actually calling the same thing that is:
/index.php/task/name/$2\-task\-$1
I have tried to do this but to no avail.
RewriteRule ^(.*)-task-(.*)\.html\?(.*)$ /index.php/task/name/$2\-task\-$1 [L]
Thank you for your help or feedback on this.
This is fixed by inserting the following code at the top of htaccess:
RewriteCond %{QUERY_STRING} (^|&)fb_comment_id=
RewriteRule ^(.*)$ /$1? [L,R=301]
basically, what it does is that it will remove any extra query string that has fb_comment_id and redirect 301 to the one without query string.
Thank you #oddant and #Gerben for helping out!

Resources