ASCII Characters in Links Producing 404 Errors - .htaccess

I used to have a query string ?q= in all my urls. I now have these urls redirecting to the alias name.
RewriteRule ^(.*)$ index.php?q=$1 [L,NC]
RewriteCond %{QUERY_STRING} ^q=(.*)$ [NC]
RewriteRule ^$(.*) /%1? [R=301,L,NE]
Unfortunately other websites link to pages on my website in this manner www.example.com/%3Fq%3Dfaqs. I changed over all the links on my site to absolute but somehow google has indexed hundreds of pages that look like www.example.com/%3Fq%3Dfaqs?q=contact or www.example.com/%3Fq%3Dfaqs%3Fq%3Dcontact.
Is there a way I can rewrite these ASCII codes back to the symbols using htaccess or some other method?

I solved this by writing lines like Redirect 301 /?q=faqs example.com/?q=faqs This redirected /%3Fq%3Dfaqs to /?q=faqs which redirected to /faqs. Probably not the best way to do it, but it works.

Related

hard time with rewriting dynamic url

I'm trying to convert the dynamic urls (with query string) of my website to friendly ones. That's the first time for me and I'm doing it for seo optimization.
I reached the point where if I type the friendly url, the browser sends me to the equivalent dinamic url (and I see the friendly url in the navigation bar), but if I navigate inside the website the navigation bar still shows me the url with the query.
Is that right?
Here's my .htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^example\.it
RewriteRule ^(.*)$ http://www.example.it/$1
RewriteRule (.*).html$ /$1.php [R=302]
RewriteRule ^ita/([A-Za-z0-9\-\+]+).php$ $1.php?lang=it [NC,L]
RewriteRule ^eng/([A-Za-z0-9\-\+]+).php$ $1.php?lang=en [NC,L]
RewriteRule ^ita/kiwishop/([A-Za-z0-9\-\+]+).php$ kiwishop.php?shop=$1&lang=it [NC,L]
RewriteRule ^eng/kiwishop/([A-Za-z0-9\-\+]+).php$ kiwishop.php?shop=$1&lang=en [NC,L]
RewriteRule ^sitemap.xml ./sitemap.php [L]
Should I also do the opposite operation (I mean set a rewriterule that rewrite the dinamic url into the friendly one)? If so, how can I do that?
Another question: which url should I put in the sitemap.xml, the friendly one or the one with the query?
You need to change all links on your site to the friendly URLs.
You should put in the sitemap whatever URL you want to show in search engines. It sounds like you want that to be the friendly URL.

.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]

Htaccess rewrites causing infinite loop

I am trying to write a redirect for a PDF file in my wordpress install that has shown up in my google webmaster tools with a load of query strings after it, what I want is to have the redirect match any number or query stings after the pdf filename and just redirect directly to the file.
RewriteRule ^(.*) http://www.example.com/wp-content/uploads/2014/08/test\.pdf$ [R=301,L]
and this also (as well as numerous different iterations)
RedirectMatch 301 ^/wp-content/uploads/2014/08/test\.pdf(.*)$ /wp-content/uploads/2014/08/test.pdf?
However they either result in a continuous redirect loop, 404 error, or still leave part of the query string
the url has any number of random query strings so something that would catch anything after the filename and just redirect to the bare file is percisely what I am looking for. here is an example of the url:
http://www.example.com/wp-content/uploads/2014/08/test.pdf?test=324234234.ffdsfewrgfdgdfg.234234.234324.2333333&fsdf=3432423.1.34234324&wer=werewrewr
or
http://www.example.com/wp-content/uploads/2014/08/test.pdf?test=324234234.ffdsfewrgfdgdfg.234234.234324.2333333&fsdf=3432423.1.34234324&wer=werewrewr&fsdf=3432423.1.34234324&wer=werewrewr&fsdf=3432423.1.34234324&wer=werewrewr
and what I want to get is just
http://www.example.com/wp-content/uploads/2014/08/test.pdf
Thanks
Try something like this with mod_rewrite.
RewriteEngine On
RewriteCond %{QUERY_STRING} .
RewriteCond %{REQUEST_URI} (.+)\.pdf$
RewriteRule ^(.*) /$1? [R=301,L]

Rewrite URL Structure

I would like to change the URL structure of my site. My current URL structure is like this:
www.domain.com/events/events.php?location=san%20francisco
I want to change the URL structure to:
www.domain.com/events/san-francisco
My XMPL site map has the URLs listed like the first example. I want the search engines to index the URL like the second example. What are all the things I need to do to achieve this? The {city} in the location parameter is dynamic depending on the user's IP address. Do I need to change my XML sitemap to list new URL structure? What do I need to put in my .htaccess file. Do I need to change the actual URLs links on my website to the new structure or can I just use .htaccess?
This should work:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /events\.php\?location=(.*)%20(.*)\ HTTP
RewriteRule ^ /events/%2-%3\? [R=302,L]
RewriteRule ^events/(.*)$ /events.php?location=$1 [L]
Changing R=302 to R=301 when you know it redirect correctly
EDIT:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /(.*)/(.*)\.php\?location=(.*)%20(.*)&lid=(.*)&slid=(.*)\ HTTP
RewriteRule ^ /%3/%4-%5/%6/%7\? [R=302,L]
RewriteRule ^(.*)/(.*)/(.*)/(.*)$ /$1/$1.php?location=$2&lid=$3&slid=$4 [L]
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /(.*)/(.*)\.php\?location=(.*)%20(.*)\ HTTP
RewriteRule ^ /%3/%4-%5\? [R=302,L]
RewriteRule ^([A-Za-z0-9]+)/([A-Za-z0-9]+)$ /$1/$1.php?location=$2 [L]
You have to use a .htaccess file similar to the one below. It will match fancy URL and forward the request to the right php file.
RewriteEngine On
RewriteRule ^([^/]*)/(.*)$ /$1/$1.php?location=$2
With this, both www.domain.com/events/events.php?location=san%20francisco and www.domain.com/events/san-francisco will lead to the same page.
But if you don't change your sitemap nor the links on your site, search engines won't know that.
You have then 2 solutions :
change all your links in your site and sitemap with the new structure URL
use a redirect rule to redirect "old" pages to the "new" ones
From what I know about SEO, the first option is the best. Or even better, a combination of both solutions : change all your links, and use a redirect 301 rule so pages already indexed by search engines will not lost their ranking and to avoid duplicate content.
RewriteRule ^([^/]*)/(.*).php?location=(.*)$ http://www.yourdomain.com/$1/$3 [R=301]

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.

Resources