301 redirect on query string? - .htaccess

i need to do some simple 301 redirects as i am creating a site fresh and using the current domain.
How ever the website URl's all start with '?'.
For example:
Redirect 301 /?q=news http://websitedomain/news/
This results in the home page being loaded with the url still the same.
Is there any way to get around this?

You need to use mod_rewrite to match against the query string :
RewriteEngine on
RewriteCond %{QUERY_STRING} ^q=([^&]+) [NC]
RewriteRule ^ http://example.com/%1? [NC,L,R]

Related

Redirect (301 or 302) for translated page not working

I want to redirect a translated page to another translated page in the htaccess file.
In the original language it works (Redirect 301 /helpie_faq/what-are-my-benefits/ /faq/#hfaq-post-1683), but the redirection of translated pages doesn't work, e.g.
Redirect 301 /helpie_faq/welche-vorteile-erhalte-ich-als-neueinwanderer/?lang=de /faq/?lang=de#hfaq-post-3001
How can I make this work out?
You need to match on the query string, which the redirect directive does not support. Instead you should use RewriteCond on the URI (REQUEST_URI) and on the query string (QUERY_STRING) and a matching RewriteRule.
RewriteEngine On
RewriteCond %{REQUEST_URI} /helpie_faq/welche-vorteile-erhalte-ich-als-neueinwanderer$
RewriteCond %{QUERY_STRING} ^lang=de$
RewriteRule ^.*$ /faq/?lang=de#hfaq-post-3001 [L,R=301]
For more examples look here Apache Redirect 301 fails when using GET parameters, such as ?blah= or here Redirecting URLs (with specific GET parameters)

Htaccess Redirects with Parameters (need to be removded)

For an migration of a new website i need to redirect /example.html?id=2 redirected to /example/new-page.html
When i create an redirect like this:
Redirect 301 /example.html?id=2 https://www.url.com/example/new-page.html
Redirect 301 /example2.html?bla=34 https://www.url.com/example/new-page2.html
Redirect 301 /eteste.html?yolo=2 https://www.url.com/example/new-page3.html
It returned into this:
https://www.url.com/example/new-page.html?id=2
etc
Change it into this doesn't work either:
Redirect 301 /example.html?id=2 https://www.url.com/example/new-page.html?
Is there something i'm doing wrong (yes! ;))
You need to use mod_rewrite to match against the query string :
RewriteEngine on
RewriteCond %{QUERY_STRING} ^id=([^&]+) [NC]
RewriteRule ^example.html$ https://www.url.com/example/new-page.html? [NC,L,R]

Redirect only one URL with parameter

I need to redirect URL, for example:
www.mydomain.com/category/sub-category/product?page=shop.ask&flypage=flypage.tpl&product_id=181&category_id=34
To:
www.mydomain.com/category/sub-category/good-product.html
I have a multiple URLs with parameters that need to be redirected to only one or couple of URLs, can you help me, I used Google for hours.
I was try this code at .htaccess:
redirect 301 /category/sub-category/product?page=shop.ask&flypage=flypage.tpl&product_id=181&category_id=34 www.mydomain.com/category/sub-category/good-product.html
But it doesn't work.
You can't match against the query string in a Redirect directive. You'll need to use mod_rewrite:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^page=shop.ask&flypage=flypage.tpl&product_id=181&category_id=34$
RewriteRule ^category/sub-category/product$ /category/sub-category/good-product.html? [L,R=301]

htacess 301 redirect with query string from page that does not exist

My old search file used to be named namesearch.php. It's now named search.php. The query string parameters remain the same. I would like to redirect any namesearch request to search.php. Here is what I have below, which is not working.
RewriteRule ^/namesearch.php%1 search.php$1 [R=301,L]
Query strings are automatically appended when the target URI doesn't try to construct its own (i.e. it doesn't have a ? in it). You can use either mod_rewrite or mod_alias:
mod_alias:
Redirect 301 /namesearch.php /search.php
mod_rewrite:
RewriteEngine On
RewriteRule ^/?namesearch.php /search.php [L,R=301]

301 Redirecting from old page to new page on Apache not working

A simple 301 redirect is not working in this instance - For example:
Redirect 301 /oldpage http://www.mysite.co.uk/newsubdir/newpage
The site is dynamic and the .htaccess is already renaming pages to search engine friendly URL's from URL's containing a query string.
RewriteRule ^(.*)/(.*)$ index.php?page_name=$1&sub=$2 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)([^/])$ http://www.mysite.co.uk/$1$2/ [R=301,L]
When we are using these 301 redirects like above in the same .htaccess (at the bottom), the pages are redirecting but the query string is getting added to the end of the URL frustratingly and we have not figured out why or how to prevent it.
After 301 redirect, URL is looking like:-
http://www.mysite.co.uk/newsubdir/newpage/?page_name=old-page&sub=
...Causing a 404 error - it's just the query string added on the end of the URL's that is breaking the redirect.
Please can anyone advise on what needs to be done to fix this?
Thanks
Add question mark ? at the end of URL to prevent existing query string to be copied to a new URL:
Redirect 301 /oldpage http://www.mysite.co.uk/newsubdir/newpage?
But since you are already using mod_rewrite, I would recommend utilising it for this task as well (place this rule above your other rewrite rules):
RewriteRule ^oldpage$ http://www.mysite.co.uk/newsubdir/newpage? [R=301,L]
redirect 301 /oldfile.htm http://www.example.com/newfile.htm
Use it. Working fine.

Resources