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

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]

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]

.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.

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]

Combined two mod rewrite htaccess rules into one

How would I combined these 2 htaccess mod_rewrite rules into one using OR.
RewriteCond %{QUERY_STRING} ^(.*)%20(.*)$
RewriteRule ^(.*)$ /$1?%1+%2 [L,R=301,NE]
RewriteCond %{QUERY_STRING} ^(.*)%2B(.*)$
RewriteRule ^(.*)$ /$1?%1+%2 [L,R=301,NE]
Is this how?
RewriteCond %{QUERY_STRING} ^(.*)%20(.*)$ [OR]
RewriteCond %{QUERY_STRING} ^(.*)%2B(.*)$
RewriteRule ^(.*)$ /$1?%1+%2 [L,R=301,NE]
or is there even a shorter way than that?
You can use regex alternation:
RewriteCond %{QUERY_STRING} ^(.*)(?:%20|%2B|\s)(.*)$
RewriteRule ^(.*)$ /$1?%1+%2 [L,R=301,NE]

.htaccess RewriteRule - Difference between [L,R] and [R,L]?

Is there any difference between flag [L,R] and [R,L] e.g:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !newdomain.com$ [NC]
RewriteRule ^(.*)$ http://newdomain.com/$1 [L,R=301]
vs
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !newdomain.com$ [NC]
RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,L]
?
No there is no difference in 2 rules.
Order of various flags in RewriteRule doesn't matter.
Reference: httpd.apache.org/docs/current/rewrite/flags.html