How to prevent duplicate content using htaccess - .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.

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]

Rewrite and Redirect with htaccess

A client has asked us to replicate all the content from an old domain (bcsbd.com) to their main domain (ywcacam.org), and also create redirects so the old URLs are still functional. Unfortunately, the URLs aren't exact matches, e.g., [olddomain]/about has become [newdomain]/about_soo_bahk_do. There are less than 10 specific URLs to handle, which we initially did successfully using Redirect statements in the old domain's htaccess file:
# redirect specific pages to page on new domain
Redirect /about http://www.ywcacam.org/about_soo_bahk_do
We also need a catch-all, so that any other requests go to a specific URL on the new domain, e.g., www.bcsbd.com/somefile becomes www.ywcacam.org/soo_bahk_do. We handled this using Rewrite statements:
# catch-all for any requests not specified above
RewriteCond %{HTTP_HOST} ^(bcsbd.com|www.bcsbd.com) [NC]
RewriteRule ^(.*)$ http://www.ywcacam.org/soo_bahk_do [L]
Quick research showed the Rewrite directives (using mod_rewrite) would always be processed before the Redirect directives (using mod_alias). So we replaced the Redirects with Rewrites:
Options +FollowSymlinks
RewriteEngine On
RewriteRule /about http://www.ywcacam.org/about_soo_bahk_do [L]
RewriteRule /programs http://www.ywcacam.org/programs_soo_bahk_do [L]
...
# catch-all for any requests not specified above
RewriteCond %{HTTP_HOST} ^(bcsbd.com|www.bcsbd.com) [NC]
RewriteRule ^(.*)$ http://www.ywcacam.org/soo_bahk_do [L]
The problem is that just the catch-all is working - the new Rewrite rules are being ignored. What are we doing wrong in those statements?
Thanks in advance for the help!

How to rewrite url for certain pages in htaccess?

I am trying to show different url and redirect users to specific url with .htcaccess when they click on a blog post but to no avail.
Lets say the url is: http://localhost/mySite/article.php?article_title=test-title
then I would like to show it as http://localhost/mySite/article/test-title
This is my current htcaccess file:
#turn on url rewriting
RewriteEngine on
#remove the need for .php extention
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
#rewrite rule for blog
RewriteRule article/([A-Za-z0-9-]+) /mySite/article.php?article_title=$1
But for some reason it is not redirecting/showing the correct url. I am not getting any errors.
EDIT
Trying to ask my question again and explain it better. Let's say the url is
http://localhost/www.example.com/admin/editUser.php?user_id=126
and I would like to rewrite the url like this:
http://localhost/www.example.com/admin/user/126
then how can I achieve this. I tried using this website to check the modified url but it does not work. Seems like it does not work with any of the accepted answers here in stack at all.
This is my htaccess file atm. It is in the root of www.example.com
#turn on url rewriting
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^user/([0-9]+)/?$ /editUser.php?user_id=$1 [NC,L] # Handle user edit requests
Apache Module mod_rewrite is enabled. Also added an alias. Still no changes in the url. If I try something really basic like this:
# redirect to .php-less link if requested directly
RewriteCond %{THE_REQUEST} ^[A-Z]+\s.+\.php\sHTTP/.+
RewriteRule ^(.+)\.php $1 [R=301,L]
it works fine.
Why is the users redirect not working? What am I doing wrong.
Try it like this for your rule for article url in mysite directory.
RewriteRule ^article/([A-Za-z0-9-]+)$ article.php?article_title=$1 [QSA,NC,L]
you need to mention start ^ and end $ of string.

htaccess redirect a url with a param and remove duplicates

I have this url
http://www.mmametals.com.php5-20.dfw1-1.websitetestlink.com/?page_id=61
and i want to redirect to the root like this
http://www.mmametals.com.php5-20.dfw1-1.websitetestlink.com
here is my htaccess
RewriteEngine on
redirect 301 /?page_id=61/ http://www.mmametals.com.php5-20.dfw1-1.websitetestlink.com
but nothing is happening...any ideas
Also I was wondering if there is a way also in .htaccess to delete a duplicate /about ...so for example if the url is
http://somesite.com/about/about
it will rewrite the rule to always be
http://somesite.com/about
RewriteEngine On
RewriteCond %{QUERY_STRING} ^page_id=61$
RewriteRule (.*) http://www.mmametals.com.php5-20.dfw1-1.websitetestlink.com? [R=301,L]
RewriteCond %(REQUEST_URI) ^/about/about[/]?
RewriteRule (.*) http://somesite.com/about? [R=301,L]
should do it.
I don't know the exact answer off the top of my head as I don't work directly with apache that much any longer, but I think you need to look into apache's mod_rewrite module. Try taking a look at:
Apache's mod_rewrite documentation
This post about blocking certain URLS

How to remove part of a URL using .htaccess

I have a url like this.
/domains/details.php (NOTE: domains is dynamic and can be anything)
How do I remove the domains part from the URL using .htaccess so the actual lookup is:
/details.php
OR it'll be cool if I can get domains into the URL.
/details.php?page=domains
Thanks!
Scott
RewriteEngine on
RewriteBase /
RewriteRule ^([^/]+)/details.php$ /details.php?page=$1 [R=301]
Leave off the [R=301] if you want an internal redirect rather than an actual HTTP redirect.
To preserve existing query parameters you can change the rule to this:
RewriteRule ^([^/]+)/details.php(.*)$ /details.php?page=$1&%{QUERY_STRING} [R=301]
Please try to use the following rules to deal with your last request:
RewriteRule ^(?!domains/.*)([^/]+)/details.php$ domains/details.php?page=$1 [R=301,QSA]
RewriteRule ^domains/details.php$ details.php [NC,L]

Resources