Redirect htacces links - apache

I want to redirect some links for not having duplicate content on google
The links are like this.. www.example.ro/adidasi_dama.php?page=1
I made the rewrite rule to .. www.example.ro/adidasi-dama/pagina-1/
with this code..
RewriteRule ^([a-zA-Z]+)([-_]){1}([a-zA-Z]+)/pagina-([0-9]+)/$ $1_$3.php?page=$4
RewriteRule ^([a-zA-Z]+)([-_]){1}([a-zA-Z]+)/$ $1_$3.php
RewriteRule ^([a-zA-Z_]+)/pagina-([0-9]+)/$ /$1.php?page=$2
But now... both versions are working... .. i want to automaticaly redirect from 1st link to second... but i need it for all my pages..
i have many links for example
www.example.ro/noutati.php?page=1
www.example.ro/adidasi_fete.php?page=1
www.example.ro/pantofi_barbati.php?page=1
...
Thank you very much!

Try adding these rules to the same htaccess file:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([a-zA-Z_]+)\.php\?page=([0-9]+)
RewriteCond %1:%2 (.+)_(.+):(.*)
RewriteRule ^ /%1-%2/pagina-%3/? [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([a-zA-Z_]+)\.php\?page=([0-9]+)
RewriteRule ^ /%1/pagina-%2/? [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([a-zA-Z_]+)\.php(\ |$)
RewriteCond %1 (.+)_(.+)
RewriteRule ^ /%1-%2/? [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([a-zA-Z_]+)\.php(\ |$)
RewriteRule ^ /%1/? [L,R=301]

Related

How to redirect almost similar links?

I need to write two redirects:
From
https://site.ru/dveri to https://anothersite.ru/dveri-iz-dereva/
From
https://site.ru/dveri?start=14 to https://anothersite.ru/blog/
I wrote two rules in htaccess:
#1
RewriteCond %{THE_REQUEST} \s/+dveri[?\s/] [NC]
RewriteRule ^ https://anothersite.ru/dveri-iz-dereva/ [L,R=301]
#2
RewriteCond %{THE_REQUEST} \s/+dveri[?\s/] [NC]
RewriteRule ^.*$ https://anothersite.ru/blog/? [L,R=301]
Result:
link https://site.ru/dveri redirects correctly to https://anothersite.ru/dveri-iz-dereva/
link https://site.ru/dveri?start=14 redirects incorrectly to https://anothersite.ru/dveri-iz-dereva/?start=14
The two rules you wrote are the same. Try this for the second
RewriteCond %{THE_REQUEST} /dveri\?start=14$
RewriteRule ^ https://anothersite.ru/blog/? [L,R=301]
My experienced collegue helped me with this:
RewriteCond %{THE_REQUEST} \s/+dveri[?\s/] [NC]
RewriteCond %{QUERY_STRING} ^start=14$
RewriteRule ^.*$ https://anothersite.ru/blog/? [L,R=301]
RewriteCond %{THE_REQUEST} \s/+dveri[?\s/] [NC]
RewriteRule ^ https://anothersite.ru/dveri-iz-dereva/ [L,R=301]
RewriteCond %{THE_REQUEST} \s/+dveri[?\s/] [NC]
RewriteCond %{QUERY_STRING} ^start=14$
RewriteRule ^.*$ https://anothersite.ru/blog/? [L,R=301]
RewriteCond %{THE_REQUEST} \s/+dveri[?\s/] [NC]
RewriteRule ^ https://anothersite.ru/dveri-iz-dereva/ [L,R=301]
If these directives are placed at the top of the root .htaccess file then these can be simplified to:
RewriteCond %{QUERY_STRING} ^start=14$
RewriteRule ^dveri/?$ https://anothersite.ru/blog/ [NC,QSD,R=301,L]
RewriteRule ^dveri/?$ https://anothersite.ru/dveri-iz-dereva/ [NC,R=301,L]

dynamic url change issue

I want to change my URL from https://website.com/free?s=new-check to https://website.com/free/new-check/
My Current htaccess code :
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/?(.*)$ /$1.php [L]
RewriteCond %{THE_REQUEST} /free\?s=([^\s]+) [NC]
RewriteRule ^ /%1? [NC,L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)$ /free?s=$1 [NC,L]
It works and directed to https://website.com/new-check but what I want is https://website.com/free/new-check/
previously I removed the .php extension from the URL.
I tried a lot of solutions but nothing works. please help me
With your shown samples, please try following htaccess Rules. Make sure to keep these Rules at top of your htaccess file if you have other Rules also in your file.
Please make sure to clear your browser cache before testing your URLs.
RewriteEngine On
##https apply rules here.
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
##External redirect Rule here.
RewriteCond %{THE_REQUEST} \s/(free)\?s=([^\s]+)\s [NC]
RewriteRule ^ /%1/%2? [L,R=301]
##Internal rewrite rule here.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?:[^/]*)/(.*)/?$ free.php?s=$1 [NC,QSA,L]

How to remove php extension form URL .htaccess

I have a code in my .htaccess file:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteRule ^(.*)\.html$ /$1 [R=301,L]
It removes .html extension from urls, how do I remove .php as well with the same code?
Check this modified rule
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.(php|html|htm)\ HTTP/
RewriteRule ^(.*)\.(php|html|htm)$ /$1 [R=301,L]
I am sure there's a shorter way to solve this out there but in this is how I remove php extension at the moment:
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ $1\.php
Update:
Try this since you would like to main existing code
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.php\ HTTP/
RewriteRule ^(.*)\.php$ /$1 [R=301,L]

301 redirect doesnot work in htaccess

I want to redirect from https://*****.com/lp/index.html
to https://*****.com/lp/
so I put these two line in .htaccess
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /lp/index\.html\ HTTP/
RewriteRule ^/lp/index\.html$ /lp/ [R=301,L]
and now the whole redirect block in my htaccess file is like this:
RewriteEngine on
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.php\ HTTP/
RewriteRule ^(([^/]+/)*)index\.php$ /$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /lp/index\.html\ HTTP/
RewriteRule ^/lp/index\.html$ /lp/ [R=301,L]
but the index.html redirect not working.
Does anyone know why? Thank you.
Use it like this:
RewriteEngine On
# remove index.php
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*)index\.php$ /$1 [L,R=301,NC,NE]
# remove /lp/index.html
RewriteCond %{THE_REQUEST} \s/+lp/index\.html [NC]
RewriteRule ^ /lp/ [L,R=301,NE]
Clear your browser cache and retest.
try this
http://www.rapidtables.com/web/tools/redirect-generator.htm
with Apache .htaccess redirect option

Remove question mark from URL

I don't have much experience with htaccess and mod_rewrite or anything, but how would one remove the ? from a URL that looks something like: site.com/?page so it looks like site.com/page ?
I'm trying this at the moment:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(index\.php)?\?([^&\ ]+)
RewriteRule ^ /%1? [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?$1 [L]
But it just redirects everything to just site.com index page, without anything else in the url.
Your backreference is incorrect. You want to match the 2nd grouping from %{THE_REQUEST}, not the first (which is index.php). So your first rule should look like:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(index\.php)?\?([^&\ ]+)
RewriteRule ^ /%2? [L,R=301]
# 2 here--------^
The 2nd grouping would be the ([^&\ ]+) match.