How to redirect almost similar links? - apache

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]

Related

How to write clean url in reverse?

I have the following in my my htaccess
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^terms-and-conditions terms.php
RewriteRule ^privacy privacy.php
RewriteRule ^thank-you thanks.php
How could I also do this in reverse such as typing /privacy.php is rewritten as /privacy.
You need to use 3 separate rules using %{THE_REQUEST} variable to avoid redirect loop:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,NE,L]
RewriteCond %{THE_REQUEST} \s/+terms\.php[\s?] [NC]
RewriteRule ^ /terms-and-conditions [R=301,L]
RewriteCond %{THE_REQUEST} \s/+privacy\.php[\s?] [NC]
RewriteRule ^ /privacy [R=301,L]
RewriteCond %{THE_REQUEST} \s/+thanks\.php[\s?] [NC]
RewriteRule ^ /thank-you [R=301,L]
RewriteRule ^terms-and-conditions/?$ terms.php [L,NC]
RewriteRule ^privacy/?$ privacy.php [L,NC]
RewriteRule ^thank-you/?$ thanks.php [L,NC]

.htaccess won't rewrite my url at all

I've been crawling forums for about 2 hours and still haven't found the solution to my problem so I am turning to you guys for help.
My URL looks like this
http://gymbirdz.com/article.php?url=benefits-of-fish-oil
I want it to look like this
http://gymbirdz.com/article/benefits-of-fish-oil
This is my .htaccess request but It does not work and still no luck fixing it :/
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?article/([A-Za-z0-9-]+)/?$ /article.php?url=$1 [NC,R=301,L]
Any help is appreciated very much! Thank You
UPDATE
This is how my .htaccess looks from above
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^article/([A-Za-z0-9\-]+)$ article.php?url=$1 [NC,R=301,L,QSA]
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
RewriteCond %{HTTP_HOST} ^www\.gymbirdz\.com$
RewriteRule ^/?$ "http\:\/\/gymbirdz\.com\/" [R=301,L]
RewriteCond %{HTTP_HOST} ^192\.254\.185\.208
RewriteRule (.*) http://www.gymbirdz.com/$1 [R=301,L]
RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
RewriteRule .* ? [F,L]
Try this rule your site root .htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} libwww-perl [NC]
RewriteRule ^ - [F,L]
RewriteCond %{HTTP_HOST} ^www\.gymbirdz\.com$ [OR]
RewriteCond %{HTTP_HOST} ^192\.254\.185\.208$
RewriteRule (.*) http://gymbirdz.com/$1 [R=301,NE,L]
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
RewriteCond %{THE_REQUEST} /article\.php\?url=([^\s&]+) [NC]
RewriteRule ^ /article/%1? [R=302,L,NE]
RewriteRule ^/?article/([\w-]+)/?$ article.php?url=$1 [NC,QSA,L]
Maybe I don't understand your question, but this is from the documentation:
Syntax: RewriteRule Pattern Substitution [flags]
This is your rule:
RewriteRule ^/?article/([A-Za-z0-9-]+)/?$ /article.php?url=$1 [NC,R=301,L]
It appears that you may have the two arguments reversed. Try something like this instead:
RewriteRule ^article.php?url=([\w-]+) /article/$1 [NC,L,R=301,QSA]
This assumes that the .htaccess file is in the same directory as article.php.

.htaccess i want rewrite letters - numbers in letters/numbers

http:/example.com/word-123 --> http://example.com/word/123
i'm trying to do in this way but nothing happen
RewriteCond %{HTTP_HOST} ^([a-z]+)/([0-9]+)$
RewriteRule ^([a-z]+)-([0-9]+)$ [P]
OR
RewriteRule http://prink-it.adit.it/([a-z]+)-([0-9]+) [P]
From what I understand of your question:
RewriteEngine On
RewriteCond %{THE_REQUEST} GET\ /([a-z]+)\-(\d+) [NC]
RewriteRule ^ /%1/%2 [R=301,L]
RewriteRule ^([a-z]+)/(\d+)$ /$1-$2 [L]
I Solved in this way
RewriteCond %{THE_REQUEST} GET\ /([a-z]+)\/(\d+) [NC]
RewriteRule ^ /%1-%2 [R=301,P]
RewriteRule ^([a-z]+)-(\d+)$ /$1/$2 [P]

.htaccess redirect subdomain before forcing HTTPS?

I have the following rules on my website:
Force HTTPS on all pages.
Redirect http://user.domain.com to https://www.domain.com/file.php?user=user
The problem I'm having now is that when someone goes to http://user.domain.com , they get redirected to https://user.domain.com , which is then insecure, instead of to https://www.domain.com/file.php?user=user .
How do I fix this?
Here is the .htaccess content:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^.*$ - [L]
RewriteCond %{HTTP_HOST} ^(.*?)\.(www\.)?domain\.com$ [NC]
RewriteRule ^.*$ https://www.domain.com/file.php?user_id=%1 [L]
I also tried to change %{HTTP_HOST} to %{HTTPS_HOST} and it ended up with the same problem.
Try with:
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^.* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^(.*?)\.(www\.)?domain\.com$ [NC]
RewriteRule ^.*$ https://www.domain.com/file.php?user_id=%1 [L]
You can use these rule:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com$ [NC]
RewriteRule ^ /file.php?user_id=%1 [L,QSA]

Bad Flag Delimiters on .htacces

I've entered the following into my .htaccess and I'm getting the dreaded bad flag delimiters error. I've tried validators, and one says it's lines 5 and 7, another says all the other lines.
Any help would be appreciated.
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9} /index.php HTTP/
RewriteRule ^index.php$ http://www.example.com/ [R=301,L]
RewriteCond %{QUERY_STRING} ^source= RewriteRule (.*) /$1? [R=301,L]
RewriteRule ^category/([^/.]+)/?$ category.php?id=$1 [L]
RewriteRule ^business/([^/.]+)/?$ business.php?id=$1 [L]
RewriteRule ^event/([^/.]+)/?$ event.php?id=$1 [L]
Space is not allowed unescaped. Try this code:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index.php\ HTTP/
RewriteRule ^index.php$ / [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /category.php\?id=([^&\ ]+) HTTP/
RewriteRule ^ /category/$1? [R=301,L]
RewriteCond %{QUERY_STRING} ^source=
RewriteRule (.*) /$1? [R=301,L]
RewriteRule ^category/([^/.]+)/?$ category.php?id=$1 [L]
RewriteRule ^business/([^/.]+)/?$ business.php?id=$1 [L]
RewriteRule ^event/([^/.]+)/?$ event.php?id=$1 [L]