Duplicate URLs and solution in htaccess - .htaccess

after years I discovered a bug in duplication URLs that I try to fix (author is gone) despite htaccess rule I thought should force it.
Issue:
I have the same content under 2 URLS, ex:
http://www.wrock.pl/news-23069/Depeche_Mode_Party_w_klubie_Liverpool_/
www.wrock.pl/index.php?s=news&d=23069&nazwa=Depeche_Mode_Party_w_klubie_Liverpool_
or shorter www.wrock.pl/index.php?s=news&d=23069
Another example are menu pages:
http://www.wrock.pl/koncerty/
www.wrock.pl/index.php?s=koncerty
I thought the "index.php" version should be hidden or redirected to friendly URL version, meanwhile both show with 200/OK
Index.php itself gets redirected OK: www.wrock.pl/index.php
Aim:
be able to show only friendly URl version (redirect the index.php version).
Google stopped indexing them so it may be an issue (one of many reasons).
Here is my htaccess responsible for presented examples:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^wrock.pl(.*) [NC]
RewriteRule ^(.*)$ http://www.wrock.pl/$1 [R=301,L]
# REDIRECT index.php to /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.php\ HTTP/
RewriteRule ^(([^/]+/)*)index\.php$ http://www.wrock.pl/$1 [R=301,L]
#NEWS
RewriteRule ^news-([0-9]+)/([^-]+)/$ index.php?s=news&d=$1&nazwa=$2 [L,NC]
RewriteRule ^news/([0-9]+)/$ index.php?s=news&strona=$1 [L,NC]
#KONCERTY
RewriteRule ^koncerty/$ index.php?s=koncerty [L,NC]
I dont know if current redirections should be changes or should I enter something like:
#RewriteRule ^index.php?s=news&d=([0-9]+)&nazwa=([^-]+)$ http://www.wrock.pl/news-$1/$2/ [L,NC]
which doesn't work of course.
Thank You very much for any suggestions.

Above your #NEWS items, you need to add these rules:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?s=news&d=([0-9]+)&nazwa=([^-&\ ]+)($|\ )
RewriteRule ^ /news-%1/%2/ [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?s=news&strona=([0-9]+)($|\ )
RewriteRule ^ /news/%1/ [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?s=koncerty
RewriteRule ^ /koncerty/ [L,R=301]
And in order to handle the "shorter www.wrock.pl/index.php?s=news&d=23069":
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?s=news&d=([0-9]+)($|\ )
RewriteRule ^ /news-%1/ [L,R=301]
RewriteRule ^news-([0-9]+)/$ index.php?s=news&d=$1 [L]

Related

htaccess multiple urls to one page redirect not working

I have a bunch of url's like https://example.com/index.php?articles_id=XX, where XX is some number. I want to
redirect all of them to https://example.com/maps.php (ignoring the parameters) but none of the redirect code I use works. When visiting the
original url, the browser just cycles until it finally times out. The second url is to a working page.
I first tried:
RewriteRule ^articles_id=(.*)$ /maps.php [L,R=301]
then
RewriteRule ^https://example.com/index.php?articles_id=(.*)$ /maps.php [L,R=301]
then
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php?articles_id=\ HTTP/
RewriteRule ^index\.php?articles_id=$ https://example.com/maps.php [R=301,L]
and finally
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php?articles_id=\ HTTP/
RewriteRule ^(.*)$ ./maps.php [R=301,L]
There is other redirect code in the file that is working, like this one:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ https://example.com/ [R=301,L]
Can someone please point out the reason why the redirects are not working?
This turned out to be code in the files that was preventing the redirect.

How to rewrite paged URL's?

I have changed my web page url from localhost/products.php to localhost/products using the code below.
RewriteCond %{THE_REQUEST} /products\.php[\s/?] [NC]
RewriteRule ^ /products [R=301,L]
RewriteRule ^products/?$ products.php [L,NC]
But when I navigate to next page, address bar shows something like:
localhost/products?page=2 which I want to appear as localhost/products/page2
Tried this RewriteRule ^products/page/([0-9]+)/?$ products.php?page=$1 [L] but this did not work.
My full code for rewriting page url is:
RewriteCond %{THE_REQUEST} /products\.php[\s/?] [NC]
RewriteRule ^ /products [R=301,L]
RewriteRule ^products/?$ products.php [L,NC]
RewriteRule ^products/page/([0-9]+)/?$ products.php?page=$1 [L]
Here how can I change the URL for next pages? I mean from localhost/products?page=2 to localhost/products/page2 and so on.
You can use the following rule :
RewriteCond %{THE_REQUEST} /products/?(?:\.php)?\?page=([^\s&]+) [NC]
RewriteRule ^ /products/%1? [R=301,L]
RewriteRule ^products/([0-9]+)/?$ /products.php?page=$1 [L,NC]
This will convert the urls
/products.php?page=number
/products/?page=number
to
/products/number

My one url is not redirecting

i want to ask that when i upload .htaccess file to my hostgator server my one of url is not working
url: email/admin/index.php
the htacces code here:
RewriteEngine on
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://www.appynator.com/ [R=301,L]
RewriteCond %{HTTP_HOST} ^appynator\.com [NC]
RewriteRule (.*) http://www.appynator.com/$1 [L,R=301]
Try changing the first rule to:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.php(\?|\ )
RewriteRule ^ http://www.appynator.com/%1 [R=301,L]

.htaccess mod_rewrite cannot remove page name

Having trouble figuring out the mod rewrite for .htaccess I want the url http://www.example.com/archive.php?title=about_me which is a dynamic url to be rewritten to http://www.example.com/about_me. I am using php and here is my current .htaccess code, however it only rewrites to http://www.example.com/archive/about_me want the archive to be removed.
Options +FollowSymLinks -MultiViews
rewriteengine on
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/archive\.php\?title=([^&\ ]+)
RewriteRule ^ /archive/%1? [L,R=301]
RewriteRule ^/?archive/(.*)$ /archive?title=$1 [L]
## Hide .php extension
## To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
I did get it to rewrite correctly with this code
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/archive\.php\?title=([^&\ ]+)
RewriteRule ^ /%1? [L,R=301]
RewriteRule ^/(.*)$ /archive?title=$1 [L]
However it then returns a page cannot be found error
I you want the /archive/ to be removed, you'll have to ensure that any URI that's in the form of /something must absolutely be routed to the archive.php script. Because there's simply no way to tell whether /my_blog is actually a request for /my_blog or whether it needs to be sent to the archive.php script with "my_blog" as the value of title in the query string. The best you can do is check that it's not a request for an existing resource via the -f and -d conditions:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/archive\.php\?title=([^&\ ]+)
RewriteRule ^ /%1? [L,R=301]
# no /archive/ ^
# condition checks
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.*)$ /archive?title=$1 [L]
Something like this should do the trick:
RewriteCond %{QUERY_STRING} ^title=(.*)
RewriteRule ^archive.php /%1?
RewriteRule ^(.*)$ /archive?title=$1 [L]
EDIT: Added the final RewriteRule as noted in my comments on the original question. Per the comment I believe you are trying to do the following two things:
Redirect any user-entered "real" URLs to the "friendly" URL: http://www.example.com/archive.php?title=about_me to http://www.example.com/about_me as stated in the question.
Rewrite the "friendly" URL to the "real" URL: http://www.example.com/about_me to http://www.example.com/archive.php?title=about_me, which was not clear as stated.

Can someone give me the code for a specific .htaccess?

I tried to make my own .htaccess file with a few thing in it, but I'a not very familiar with it. I googled a lot but at the end I only managed to mess things up.
So, can someone give me working .htaccess code snippet?
I want it to do the following:
redirect from non-www to www
redirect index.php to / in all directories
remove ".php" from all files in all directories
redirect URL's like product.php?detail=Phone to product/detail/Phone
(also manage the trailing slash problem)
Hope someone can help.
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
#non-www to www
RewriteCond %{HTTP_HOST} ^nevca.getfreehosting.co.uk [NC]
RewriteRule ^(.*)$ http://example.co.uk/$1 [R=301,L]
# index.php to /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)index\.php$ /$1 [R=301,L]
#index/menu/123/ to index.php?menu=123
RewriteRule ^index/([^/]+)/?$ /index.php?menu=$1 [L]
#index.php?menu=123 to index/menu/123/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index2\.php\?menu=([^&]+)\ HTTP/
RewriteRule ^index\.php$ http://example.co.uk/index/%1/? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php
# Forces a trailing slash to be added
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
Something like this, just the last 2 rules don't work...
And I also want the .php to be removed only when request is not like ".php?..."
There are some of these thing in the html5 boilerplate htaccess, you should take a look and use the part you want:
http://html5boilerplate.com/

Resources