.htaccess redirect URL starts with /?/ - .htaccess

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]

Related

.htaccess help! redirecting URL with query string to root

I'm new to htaccess so please bear with me..
I'm trying to redirect traffic to URLs with query string to the root of my site as these URLs are no longer used.
Here is an example:
I would like traffic to this URL
https://www.DomainName.com/subdir1/subdir2/cond_e.asp?obark=110246
to go to http://www.DomainName.com
I do not need to keep the URL or anything just redirect to my homepage.
I need to do this for query string value from obark=110246 all the way up to obark=120000.
I've basically played around with the below to get at least one URL to redirect properly but never could get it to work at all.
RewriteCond %{QUERY_STRING} ^oPark=110246$
RewriteRule ^subdir1/subdir2/cond_e(.*) ^$? [NE, R=301, L]
What am I doing wrong? please help!
Try with below rule in subdir2,
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/cond_e.asp
RewriteCond %{QUERY_STRING} ^obark=[1]{1}[1-2]{1}[0]{1}[02-9]{1}[094-5]{1}[06-9]{1}
RewriteRule ^ http://www.DomainName.com? [R=301,L]

Rewriting old sites urls to new site's urls

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.

Redirect russian URL in htaccess doesn't work

I'm changing my URL structure in my Wordpress website. So I'm using the .htaccess file to redirect the URLS. When I add the following code in .htaccess the url www.mydomain.com/test/?lang=en redirects correctly to www.test.com
RewriteEngine On
RewriteCond %{QUERY_STRING} ^lang=en$ [NC]
RewriteRule ^test/$ http://www.test.com/? [R=301,NE,NC,L]
My website is also in Russian.
My goal is to redirect www.mydomain.com/шарон/?lang=RU to www.test.com.
I tried to add the following code to .htaccess:
RewriteCond %{QUERY_STRING} ^lang=RU$ [NC]
RewriteRule ^%D1%88%D0%B0%D1%80%D0%BE%D0%BD/$ /www.test.com? [R=301,NE,NC,L]
But the redirect doesn't work. I got a "can't display the page" error, I think a 404 error.
I also tried to add the Russian text into the .htaccess file. And saved the .htaccess to UTF-8 file format.
RewriteCond %{QUERY_STRING} ^lang=RU$ [NC]
RewriteRule ^шарон/$ /www.test.com? [R=301,NE,NC,L]
Then I get the message below, my website isn't reachable anymore.
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator to inform of the time the error occurred and of anything you might have done that may have caused the error.
More information about this error may be available in the server error log.
Does anyone have an idea how to redirect my Russian urls?
I'm not sure if URL can be specified that way when doing rewrites. Try changing it to
RewriteCond %{QUERY_STRING} ^lang=RU$ [NC]
RewriteRule ^\xd1\x88\xd0\xb0\xd1\x80\xd0\xbe\xd0\xbd/$ /www.test.com? [R=301,NE,NC,L]
Where
\xd1\x88\xd0\xb0\xd1\x80\xd0\xbe\xd0\xbd
equals to
%D1%88%D0%B0%D1%80%D0%BE%D0%BD
and therefore also to
шарон
Hope it helps. Comment on this if you have further problems. ;)

Trying to redirect one page to another, but my redirect rule doen't respond

I've removed and/or combined a couple of pages on a site, And now I need to set up a 301 redirect.
I thougt doing so in my .htaccess was my best bet, but the rules I trying to add doesen't get noticed or something. They don't respond at all...
These are the rules I've tried so far:
Redirect 301 /?Page=sPage&sPage=Our-Store %{SERVER_NAME}?Page=sPage&sPage=About-Us
RewriteRule ^/?Page=sPage&sPage=Our-Store$ %{SERVER_NAME}?Page=sPage&sPage=About-Us[R=301,NC,L]
RewriteCond %{HTTP_HOST} !^%{SERVER_NAME}$ [NC]
RewriteRule . %{SERVER_NAME}%{REQUEST_URI}?Page=sPage&sPage=About-Us [R=301,L]
This last one messed up the CSS and JS src's...
I have this at the top:
RewriteEngine On
RewriteBase /
Any suggestion?
UPDATE : follow up question
I have like 3000+ equal url strings with an ending ID that is different. How do I redirect all those requests?
This is the old url : ?Page=Tuninglist&Car=*
And this is the new one : ?Page=Tuning&view=vehicle&type=Car&id=*
* The value of id= is just integers...
Was hoping something like this could work, but no - got a 500 server error instead...
RewriteCond %{QUERY_STRING} ^Page=Tuninglist&Car=([0-9]+)$
RewriteRule ^ ?Page=Tuning&view=vehicle&type=Car&id=$1 [R=301,L]
*EDIT: The 500 server error occurred because I had a ? at the beginning of the condition.
The redirect now works, but the ending id value doesn't get included.
All I get is the correct page, but not the associated content based on that id...
You can't match against the query string in a redirect or rewrite rule, you need to do it using the %{QUERY_STRING} variable in a condition:
RewriteCond %{QUERY_STRING} ^Page=sPage&sPage=Our-Store$
RewriteRule ^ %{REQUEST_URI}?Page=sPage&sPage=About-Us [R=301,L]

.htaccess redirect from GET variable to url string

I need to redirect
/search?keywords=somesearchterm
to
/search/somesearchterm
This seems incredibly basic but I've been pounding my head against it for an hour.
Thanks for taking the time to look at this.
You want to implement what is called a "301 Redirect" with mod_rewrite.
RewriteEngine ON
RewriteRule ^/search\?keywords=somesearchterm$ /search/somesearchterm
adding regular expressions:
RewriteEngine ON
RewriteRule ^/search\?keywords=(.+) /search/$1 [R=301,L]
R=301 means provide a 301 Header redirect so the user's URL changes in the browser, and L means don't process any more rewrite rules if this one matches.
If you want to do the reverse -- in other words, if someone goes to mysite.com/search/asearchterm and you want the URL to stay the same, but "behind the scenes" you want it to load a certain server script, do this:
RewriteEngine ON
RewriteRule ^/search/(.+) /search.php\?keywords=$1 [L]
You can not match aginst Query string in RewriteRule directive. Use %{THE_REQUEST} or %{QUERY_STRING} server variables to match the Query string :
The following rule works fine for this redirection
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/search\?kewords=([^&\s]+) [NC]
RewriteRule ^ /search/%1? [NE,NC,R,L]
RewriteRule ^search/([^/]+)/?$ /search?keyword=$1 [QSA,NC,L]

Resources