.htaccess won't rewrite my url at all - apache

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.

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]

MOD_REWRITE - URL ENDING TRAILING SLASH

Options -Indexes +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*)$ [NC]
RewriteCond %{HTTP_HOST} !^(.*)\.(.*)\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ HTTP%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]
ServerSignature Off
RewriteRule ^cookie-policy/?$ cookie_policy.php [NC,L]`
Hello I'm struggling really hard with URL closing trailing slash.
RewriteRule ^cookie-policy/?$ cookie_policy.php [NC,L]
I would like, for example, to force a redirection from www.website.com/cookie-policy to www.website.com/cookie-policy/
Use this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)([^/])$ http://%{HTTP_HOST}/$1$2/ [L,R=301]
It will add a / to end of your URLs. Make sure you clear your cache before testing this.

.htaccess friendly url´s issue

I am implementing friendly URLs on my website.
At the moment I have some rules working but still need help to implement the last ones.
Rules working:
RewriteRule ^SobreNos$ /about.php [L]
RewriteRule ^Contactos$ /contact.php [L]
Rule Working but not as I wanted:
RewriteRule ^catalogo-([a-zA-Z0-9]+)$ catalogo.php?cat=$1
Rule not working:
RewriteRule
^products/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ product.php?id=$1&ref=$2&cat=$3 [L]
About the 3rd rule, I wanted the friendly URL catalogo/homem and tried to change the - for the / but didn't work.
The 4th rule don´t work at all, with - or the /.
Could be a problem of the server's version? This is the version of the server "Apache ver.2.2.19 (Unix)".
My .htaccess file
RewriteBase /
Options -Indexes
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteRule ^SobreNos$ /about.php [L]
RewriteRule ^Contactos$ /contact.php [L]
RewriteRule ^catalogo-([a-zA-Z0-9]+)$ catalogo.php?cat=$1
RewriteRule
^products/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ product.php?id=$1&ref=$2&cat=$3 [NC,L]
I have reformatted your file to the following:
Options -Indexes
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteRule ^SobreNos$ about.php [L]
RewriteRule ^Contactos$ contact.php [L]
RewriteRule ^catalogo/([a-z0-9]+)/?$ catalogo.php?cat=$1 [NC,L]
RewriteRule ^products/([a-z0-9-]+)/([a-z0-9-]+)/([a-z0-9-]+)/?$ product.php?id=$1&ref=$2&cat=$3 [NC,L]
Here's what I've changed:
Moved your RewriteBase down below RewriteEngin on
Removed the leading slashes in the destinations for each rule (they are not needed because RewriteBase is set)
Removed A-Z from your expressions (because the NC, no case, flag is set)
Used the slash in the 3rd and 4th rules, and appended an optional ending slash.
My test show that everything works properly. The only reason a 404 would be thrown is if the file you are rewriting to (product.php for example) does not exist.
I have the solution for the problem.
Options -Indexes
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ catalogo.php?cat=$1 [L,QSA]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/.]+)/([^/]+)$ catalogo.php?cat=$1&scat=$2 [L,QSA]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)$ product.php?cat=$1&scat=$2&name=$3&id=$4 [L,QSA]
Still have to add a rewrite rule for about.php and contactos.php, but not a huge problem.

Multi-Country Using .htacces

I am trying to achieve any one of the following:
http://ca.domain.com/Category/SubCategory -> http://www.domain.com/index.php?country=ca&category=Category&subcategory=Subcategory
http://www.domain.com/ca/Category/SubCategory -> http://www.domain.com/index.php?country=ca&category=Category&subcategory=Subcategory
I have browsed around but unfortunately haven't been able to figure out any working solution. For the first kind I tried the following code:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com
RewriteRule ^(.*)/$ http://www.domain.com/index.php&country=%1&category=$1 [L]
The issue with sub domain is that it doesn't redirect anywhere at all. I read around and it says I need to setup wildcard virtual host. I haven't been able to figure out how to do that on godaddy. So not sure how to do it.
As for the second kind, I used the following code:
RewriteEngine on
RewriteRule ^(.*)/?(.*)/ home.php?country=$1&category=$2 [B]
But that didn't work for me either. Any help in this regard would be highly appreciated. Thank you
As for Category & Subcategory, Subcategory is kind of mandatory. So it should also work for following link:
http://www.domain.com/ca/Toronto/Programming -> http://www.domain.com/index.php?country=ca&location=Toronto&subcategory=Programming
http://www.domain.com/ca/Programming -> http://www.domain.com/index.php?country=ca&location=&subcategory=Programming
I am not quite sure as to how to tell .htaccess if it is $1=subcategory or $2=subcategory since they switch in scenario 1 & 2.
Thanks to immense help from #anubhava I have reached to the following:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteCond %{HTTP_HOST} ^((?!www\.)[^.]+)\.domain\.com$ [NC]
RewriteRule ^([^/]+)/?$ adlisting.php?country=%1&category=$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^((?!www\.)[^.]+)\.domain\.com$ [NC]
RewriteRule ^([^/]+)/([^/]+)/?$ adlisting.php?country=%1&location=$1&category=$2 [L,QSA]
RewriteCond %{HTTP_HOST} ^(www\.)?\.domain\.com$ [NC]
RewriteRule ^([a-z]{2})/([^/]+)/?$ adlisting.php?country=$1&category=$2 [L,QSA,NC]
RewriteCond %{HTTP_HOST} ^(www\.)?\.domain\.com$ [NC]
RewriteRule ^([a-z]{2})/([^/]+)/([^/]+)/?$ adlisting.php?country=$1&category=$2&subcategory=$3 [L,QSA,NC]
Have these rules in root .htaccess:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteCond %{HTTP_HOST} ^((?!www\.)[^.]+)\.domain\.com$ [NC]
RewriteRule ^([^/]+)/?$ index.php?country=%1&category=$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^((?!www\.)[^.]+)\.domain\.com$ [NC]
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?country=%1&category=$1&subcategory=$2 [L,QSA]
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^([a-z]{2})/([^/]+)/?$ index.php?country=$1&category=$2 [L,QSA,NC]
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^([a-z]{2})/([^/]+)/([^/]+)/?$ index.php?country=$1&category=$2&subcategory=$3 [L,QSA,NC]

htaccess Redirect Loop only on Home Page

All of a sudden this morning I'm having a problem with accessing the home page to my site.
I didn't make any changes so I'm not sure what the issue is. I'm guessing it has something to do with my .htaccess file.
All other pages to my site, I can access without a problem.
Below is my htaccess file. Any help would be appreciated. Thanks in advance.
Options -Indexes
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^hats-n-caps/(.*)$ http://www.domain-domain.com/apparel/hats-n-caps/$1 [R=301,L]
RewriteRule ^electronics/laser-pointers/(.*)$ http://www.domain-domain.com/desktop-office/laser-pointers/$1 [R=301,L]
#RewriteCond %{HTTP_HOST} !^www.domain-domain.com$ [NC]
#RewriteRule ^(.*)$ http://www.domain-domain.com/$1 [L,R=301]
RewriteCond %{REQUEST_URI} (^(.*)/site-map)
RewriteRule ^site-map$ /inc/pages/site-map.php [L]
RewriteCond %{REQUEST_URI} (^(.*)/site-map/products-([0-9]+))
RewriteRule ^site-map/products-([0-9]+)$ /inc/pages/site-map.php?type=product&page=$1 [L]
RewriteCond %{REQUEST_URI} (^(.*)/site-map/catalogs-([0-9]+))
RewriteRule ^site-map/catalogs-([0-9]+)$ /inc/pages/site-map.php?type=catalog&page=$1 [L]
RewriteCond %{REQUEST_URI} (^(.*)/site-map/pages-([0-9]+))
RewriteRule ^site-map/pages-([0-9]+)$ /inc/pages/site-map.php?type=pages&page=$1 [L]
RewriteCond %{REQUEST_URI} (^(.*)-p-(.*).html*)
RewriteRule ^(.*)-p-(.*).html$ product_info.php?products_id=$2&%{QUERY_STRING}[L]
RewriteCond %{REQUEST_URI} (^(.*)-c-(.*).html*)
RewriteRule ^(.*)-c-(.*).html$ product_info.php?catalog_id=$2&%{QUERY_STRING}[L]
RewriteCond %{REQUEST_URI} !(^(.*)-c-(.*).html*|^(.*)-p-(.*).html*|^/index.php|^/sitemap.xml.*|^/info.php.*|^/google_base_feed.xml.*|^/BingSiteAuth.xml.*|^/test.html.*|^/test.*|^/ajaxplorer.*|/livechat.*|^/phpmyadmin.*|^/ControlPanel.*|^/actikare.*|^/amwater.*|^/phi.*|^/sas.*|^/blog.*|^/cfscg.*|^/chase.*|^/cleaver.*|^/clubz.*|^/medicone.*|^/net2ftp.*|^/college-net.*|^/ducks.*|^/fast-teks.*|^/gatesgroup.*|^/guesthouse.*|^/neighborcare.*|^/pdf.*|^/reedrill.*|^/survey.*|^/temp.*|^/templates.*|^/tidewater.*|^/tuthill.*|^/uploads.*|^/upload.*|^/_include.*|^/product_info.*|^/server_request.*|^/server_request_products.*|^/admin.*|^/img/.*|^/images/.*|^/spaw2?|^/static/.*|^/tmp/|^/_include/|^/robots.txt|^/LiveSearchSiteAuth.xml|^/favicon.ico|/SiteReturn/.*|^/inc/pages/site-map.php(.*)|^/carousel_products.php)
RewriteRule (.*)$ /index.php [L]
RewriteCond %{HTTP_HOST} ^domain-domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.domain-domain.com$
RewriteRule ^/?$ "\/" [R=301,L]
RewriteCond %{HTTP_HOST} ^domaindomain\.co$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domaindomain\.co$
RewriteRule ^/?$ "\/" [R=301,L]
This line:
RewriteRule ^/?$ "\/" [R=301,L]
Is what's causing your redirect loop. Get rid of the entire rule:
# RewriteCond %{HTTP_HOST} ^domaindomain\.co$ [OR]
# RewriteCond %{HTTP_HOST} ^www\.domaindomain\.co$
# RewriteRule ^/?$ "\/" [R=301,L]
It essentially redirects requests to the root / to itself.