hard time with rewriting dynamic url - .htaccess

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.

Related

Redirect already SEO friendly URL to another SEO friendly URL externally using .htaccess

Part of my .htaccess code is as follows:
RewriteRule ^([A-Z]+)*$ nextlevels_state.php?state=$1 [L]
RewriteRule ^([A-Z]+)*/([0-9]+)$ nextlevels_state.php?state=$1&page=$2 [NC]
This basically redirects URL, example.com/XX to example.com/nextlevels_state.php?state=XX and example.com/XX/1 to example.com/nextlevels_state.php?state=XX&page=1 (internally).
Now, I would like to change the URL structure of the URL from example.com/XX to example.com/nextlevels/XX and example.com/XX/1 to example.com/nextlevels/XX/1
and I tried changing .htaccess to as follows:
RewriteRule ^nextlevels/([A-Z]+)*$ nextlevels_state.php?state=$1 [L]
RewriteRule ^nextlevels/([A-Z]+)*/([0-9]+)$ nextlevels_state.php?state=$1&page=$2 [NC]
However, as the site urls are already indexed in search engines, I would like to know a way to redirect all the traffic from example.com/XX to example.com/nextlevels/XX (externally) using .htaccess .
Please guide me in this regard. Thank you community :)
Could you please try following, written and tested with shown samples only(improving your already done attempts here). Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##Rule for redirect to url example.com/nextlevels/XX.
RewriteRule ^([A-Z]+)$ nextlevels/$1 [R=301]
RewriteRule ^nextlevels/([A-Z]+)/?$ nextlevels_state.php?state=$1 [NC,L]
##Rule for redirect to url example.com/nextlevels/XX/1.
RewriteRule ^([A-Z]+)*/([0-9]+)$ nextlevels/$1/$2 [R=301]
RewriteRule ^nextlevels/([A-Z]+)*/([0-9]+)$ nextlevels_state.php?state=$1&page=$2 [NC,L]

How to prevent duplicate content using htaccess

I'm using a .htaccess to create a clean URL but
when I checked my site
example.com/some-request it has duplicate content with
xyz.com/product_view.php?category_id=some-request
I'm finding hard how to fix this and this is my rewrite rule:
RewriteEngine on
RewriteBase /
RewriteRule ^([0-9a-zA-Z_-]+)$ product_view.php?category_id=$1 [NC,L]
What can be a better solution for this?
You need to externally redirect requests for
example.com/product_view.php?category_id=some-request
to xyz.com/some-request
before your internal rewrite.
Try the following:
RewriteEngine on
RewriteBase /
# Redirect direct requests for the real URL to your desired URL
RewriteCond %{THE_REQUEST} \?category_id=([^\ ]*)
RewriteRule ^product_view\.php /%1? [R=301,L]
# Existing rewrite to real URL
RewriteRule ^([0-9a-zA-Z_-]+)$ product_view.php?category_id=$1 [NC,L]
Bit of an aside... but it's only really duplicate if these URLs get indexed. If you have always linked to your "pretty" URL then the risk is relatively low. This can also be avoided by setting the appropriate rel="canonical" link element on your pages.

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]

ASCII Characters in Links Producing 404 Errors

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.

mod_rewrite so that first-level subdirectory is a GET variable

Alright, title is REALLY sloppy.
Here's my problem: I have a news site and when you go to the main page (domain.com) it redirects you to domain.com/news/top?geography=San_Francisco after it figures out your geography.
How do I use the .htaccess so that it goes from domain.com/news/top?geography=San_Francisco domain.com/San_Francisco/news/top ?
There are some similar questions, but I have not found one similar enough in that you're editing the URL as a furtherback subdirectory.
It should also be noted that I am using the Code Igniter framework for PHP and it normally has it as domain.com/index.php/news/top?geography=San_Francisco but I did a mod_rewrite already to get rid of the index.php. The code is as follows for that:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Code I've tried:
RewriteRule ^([^/]+)/news/top$ /news/top?geography=$1 [L,QSA]
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Before the index.php rule that you have, try adding this:
RewriteRule ^([^/]+)/news/top$ /news/top?geography=$1 [L,QSA]
You'll need to make sure the links you generate are in the form of domain.com/San_Francisco/news/top though.
But to take care of the links in the wild that still look like the old way, you have to match against the actual request:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /news/top\?geography=([^&]+)
RewriteRule ^news/top$ /%1/news/top? [L,R=301]
This will 301 redirect the browser if someone goes to the link domain.com/news/top?geography=San_Francisco and make it so the browser's address bar says this: domain.com/San_Francisco/news/top. At which point the browser will send another request for the second URL, and you use the rule above to change it back into the one with a query string.

Resources