Rewriting old sites urls to new site's urls - .htaccess

The old site's urls are like:
example.com/dsk/newslong.php?id=5206
New site's urls are like:
example.com/dsk/?p=5206
The .htaccess below is not working in any way.
And more, i have 5K posts to map, and would like to have it working like a macro:
(every number after http://www.example.com/dsk/newslong.php?id=XXX should be rewrited to /dsk/?p=XXX)
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^/dsk/newslong.php?id=5206(.*)$ http://www.example.biz/dsk/?p=5206$1 [r=301,nc]
The server once installed newsite is returning a 404 not found on newslong.php.

RewriteEngine on
RewriteCond %{REQUEST_URI} ^/dsk/newslong.php$
RewriteCond %{QUERY_STRING} ^(id=[0-9]+)$
RewriteRule .* /dsk/?%1 [L,QSA]
RewriteRule is matching URI only, you can't attempt matching query string there.
But you can match it within RewriteCond condition...
That being said, if you don't care about query string content, but want all the urls that pointed to newslong.php redirected to a new location, then it's much simpler:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^id=[0-9]+$
RewriteRule ^dsk/newslong\.php$ /dsk/ [L,R]
If You omit the R, then the browser won't be redirected (the user will keep seeing old url) and instead the content will be served via sub-request.

Related

Problem with using Rewriterule in htaccess

With RewriteRule I've always cleaned my URLs as following:
RewriteRule ^page/(.*)$ page.php?urlkey=$1 [QSA]
My new host doesn't allow me to use Options +FollowSymLinks and therefore I cannot use the / anymore. So I've changed my RewriteRule to:
RewriteRule ^page-(.*)$ page.php?urlkey=$1 [QSA]
However, I need to redirect all my former URLs to the new version. I tried doing this using the following rule:
RewriteRule ^page/(.*)$ page-(.*)$ [R=301,L]
This is however not working. I've also tried to just make a Redirect in my .htaccess:
Redirect 301 https://www.example.com/page/urlkey https://www.example.com/page-urlkey
This is also not working.
EDIT
As requested the actual code below:
RewriteEngine on
RewriteRule ^citywalk-(.*)$ citywalk.php?urlkey=$1 [QSA]
RewriteRule ^citywalk/(.*)$ citywalk-(.*)$ [R=301,L]
For example the citywalk The Historical Centre has a urlkey the-historical-centre. The old url is citywalk/the-historical-centre.
To test this specific case and other technique:
Redirect 301 /citywalk/the-historical-centre https://example.com/citywalk-the-historical-centre
By visiting https://example.com/citywalk/the-historical-centre no redirecting takes place (the url stays the same in the browser) and no urlkey is found.

.htaccess 301 redirect to new domain by editing path

I need to redirect old domain to new with .htaccess
Situation:
www.oldodmain.com/en/categoryA/product1
www.newdomain.com/en/categoryB
Result I am trying to achieve
www.newdomain.com/en/categoryB/categoryA/product1
Tried to do with this code:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?voniospasaulis\.lt$ [NC]
RewriteRule ^.+$ http://www.visaslabas.lt/lt/vonios-iranga/%{REQUEST_URI} [L,R=301]
But I get a result as follow:
www.newdomain.com/en/categoryB/en/categoryA/product1
I need to get rid of /en/before/categoryA to get url like this:
www.newdomain.com/en/categoryB/categoryA/product1
Your rule pattern has to start with en/ and should capture value after en/ in $1 that you can use in target:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(?:www\.)?oldodmain\.com$ [NC]
RewriteRule ^en/(.+)$ http://www.newdomain.com/en/categoryB/$1 [L,NE,NC,R=301]
Also note that I have answered using dummy domain names instead of your actual domain names shown in question.
Make sure to clear your browser cache or use a new browser for testing.

.htaccess redirect URL starts with /?/

Google Search Console is saying that a site we recently rebuilt has a bunch of 404s, which all start with /?/, for example, /?/AboutUs.
When I redirect like:
RewriteRule ^?/AboutUs$ /about [L,R=301]
I get internal server error.
I tried it with the query string answer, that didn't help either (the URL I want to direct is not a query, I don't believe).
All of my server errors are of this kind, so a solution would be great.
?/text is part of QueryString in your url. You can not match against querystring in RewriteRule's pattern. You need to use a RewriteCond
RewriteEngine on
RewriteCond %{QUERY_STRING} ^/aboutUs [NC]
RewriteRule ^ /about [L,R=301]

Modify query string in .htaccess

I am currently updating our website and want to redirect from old url's to the new ones. I appliled several rewriterules to get nice urls without query strings for the new pages, this works perfectly. But i ran into problems when trying to do the following:
I want to change an url's query string from
www.domain.com/?content=foo
to
www.domain.com/index.php?content=bar
or even better, rewrite directly to the new , nice url i have already generated by rewrite rules:
to
www.domain.com/niceurl
in the .htaccess file using a RewriteRule.
I tried this:
RewriteCond %{QUERY_STRING} content=foo
RewriteRule content=bar$ content=foo [L]
or
RewriteCond %{QUERY_STRING} content=foo
RewriteRule niceurl$ content=foo [L]
and a lot of other variations, i just can't get it to work. Any ideas?
If you want to redirect, i.e. tell the client to forget about the old URL and fetch the new shiny URL instead, you must look for the old one (RewriteRule pattern / and RewriteCond QUERY_STRING) and then redirect R to the new URL
RewriteCond %{QUERY_STRING} content=foo
RewriteRule ^$ /niceurl [R,L]

301 Redirects problem - urls with ? and =

I'm new with 301 redirects through .htacces.
I can get simple redirects as
redirect 301 /test.html http://www.domain.com/test2.html
to work but I have some urls like this
redirect 301 /test.asp?Group=100 http://www.domain.com/test3.html
and for some reason these don't work.
Thanks.
Here is set of rules for URLs you have provided:
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} =group=113 [NC]
RewriteRule ^group\.asp$ http://domain.dk/til-born.htm? [NC,R=301,L]
RewriteCond %{QUERY_STRING} =product=1136 [NC]
RewriteRule ^product\.asp$ http://www.domain.dk/til-born/bukser.html? [NC,R=301,L]
As you can see query string is matched separately to the page name. So .. for each of such redirects you need 2 lines: RewriteCond & RewriteRule.
The rule above will do EXACT match, which means /group.asp?group=113&param=value will not be redirected because query string is group=113&param=value which is more than just group=113.
To have such redirect working (when there are some optional parameters in query string) you have to modify it: RewriteCond %{QUERY_STRING} (^|&)group=113(&|$) [NC] -- this will match group=133 anywhere in query string (group=113 and group=11366 are still different, so no problems here).
This needs to be placed in .htaccess in website root folder. If placed elsewhere some tweaking may be required.
The Redirect directive (as far as I know) matches only on the path, not querystring. Instead, use RewriteRule. The QSA instructs the rewrite engine to append the querystring onto the new redirected URL.
RewriteEngine On
RewriteRule ^test\.asp http://www.domain.com/test3.html [L,R=301,QSA]

Resources