.htaccess not working properly - .htaccess

I changed .htacess file as below.
RewriteCond %{REQUEST_URI} (.*)product-list.php(.*)
RewriteRule (.*) www.example.com/swimming-pool/product-list\.php$1 [R=301,L]
RewriteCond %{REQUEST_URI} (.*)product-info.php(.*)
RewriteRule (.*)\?(.*)$ www.example.com/swimming-pool/product\-info\.php$2 [R=301,L]
i just need that when i request for
http://www.example.com/product-info.php?Applepc.html should be redirected to
http://www.example.com/swimming-pool/product-info.php?Applepc.html
Output is getting like this in URL field:-
www.example.com/swimming-pool/product-list.phpswimming-pool/product-list.phpswimming-pool/product-list.phpswimming-pool/product-list.phpswimming-pool/product-list.phpswimming-pool/product-list.phpswimming-pool/product-list.phpswimming-pool/product-list.phpswimming-pool/product-list.phpswimming-pool/product-list.phpswimming-pool/product-list.phpswimming-pool/product-list.phpswimming-pool/product-list.phpswimming-pool/product-list.phpswimming-pool/product-list.phpswimming-pool/product-list.phpswimming-pool/product-list.phpswimming-pool/product-list.phpswimming-pool/product-list.phpswimming-pool/product-list.phpproduct-list.php?Flowers-pg1-cid38.html
Please tell me where i am mistaking.

Although you don't mention what is the purpose of the other rule with product-list.php, it is included in this rule-set.
You may try this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (product-list|product-info)\.php [NC]
RewriteRule ^(.*)/?$ swimming-pool/$1 [R=301,L]
Redirects permanently any URL like this one
http://www.example.com/product-info.php?query or
http://www.example.com/product-list.php?query
To
http://www.example.com/swimming-pool/product-info.php?query or
http://www.example.com/swimming-pool/product-list.php?query
It seems the problem with the actual rules is they are generating a loop.

Related

.htaccess redirect to subfolder, non-www to www and https

So I'm having this website which is stored inside public_html/www/. Some other domains are stored in -for example- public_html/test/. In public_html I have a .htaccess file with a LOT of redirects in it, which all apply to this website inside /www/ folder. Inside the /www/ folder I have another .htaccess (I don't really know why, my colleague did this and he left me with this without information). Since I have no idea how .htaccess exactly works, I have no idea what to do and in which file.
What I need to do is to redirect domain.nl to https://www.domain.nl (some folders should be excluded, like you can see in the code below - emailtemplates, paymentupdate, autocode, nieuwsbrieven, etc.) But domain.nl is stored inside public_html/www/ folder, so it also needs a redirect to the subfolder (which should not be visible in web browser).
At this moment, in the root .htaccess file is the following:
# Use HTTPS
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.nl [NC]
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/emailtemplates
RewriteCond %{REQUEST_URI} !^/paymentupdate_ideal
RewriteCond %{REQUEST_URI} !^/autocode
RewriteCond %{REQUEST_URI} !^/nieuwsbrieven
RewriteCond %{REQUEST_URI} !^/bevestig-aanmelding-nieuwsbrief
RewriteCond %{REQUEST_URI} !^/bevestig-afspraak
RewriteCond %{REQUEST_URI} !^/images/personal
RewriteCond %{REQUEST_URI} !^/uploadify
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Link to subfolder
# RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?www.domain.nl$
RewriteCond %{REQUEST_URI} !^/www/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /www/$1
RewriteCond %{HTTP_HOST} ^(www.)?www.domain.nl$
RewriteRule ^(/)?$ www/index.php?page=home [L]
# Non-www to www
#activate rewrite engine
#RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?domain.nl
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
Probably half of this code isn't even necessary, but I have absolutely no clue. The other .htaccess file, within the subfolder /www/, has the following text which I don't really understand (it contains more, but nothing relevant):
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://www.domain.nl/$1/ [L,R=301]
I'm sorry if I sound stupid. I've tried many things copied from internet, but unfortunately everything has its concequences, which results in other problems with our website (links that don't work anymore, such as). I think the reason it doesn't work all the time, is because it's stored in the subfolder and I don't know how to combine all those lines in one like it would work.
Can anybody help me explain how to fix this and what is going wrong at this moment? Because the website is now not redirecting to www. It's just redirecting to https which gives me a security error at this moment (because the SSL is stored on www.). SUPER thanks in advance!
My problem was wrong order of the checks. First, I redirected to https://, but when I did not use www., it did not redirect to https://www OR it redirected to https://www.www. I fixed my problem by re-ordering my rules, like below:
# Non-www to www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
# Permanent redirect to https://
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/excluded_folder1/
RewriteCond %{REQUEST_URI} !^/excluded_folder2/
RewriteCond %{REQUEST_URI} !^/excluded_folder3/
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Use subfolder for domain
RewriteCond %{HTTP_HOST} ^(www.)?www.domain.nl$
RewriteCond %{REQUEST_URI} !^/www/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /www/$1
RewriteCond %{HTTP_HOST} ^(www.)?www.domain.nl$
RewriteRule ^(/)?$ www/index.php?page=home [L]

Remove duplicate content issue using htaccess

My .htaccess file looks likes this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?pg=$1 [L]
When a non-file or non-dir url is requested, it works fine.
But if index.php?pg=(pagename) is requested, it also works which can possibly lead to duplicate content issue.
Is there a way to prevent this?
You're right, this can lead to duplicate content.
And, yes, it is possible to avoid it.
This solution redirects old url to new url equivalent, for instance http://website.com/index.php?pg=get/my/page/please.html to http://website.com/get/my/page/please.html.
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \s/index\.php\?pg=([^&\s]*)\s [NC]
RewriteRule ^ %1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?pg=$1 [L]

URL Rewriting syntax IIS 8 ISAPI Rewrite 3

I had a previous post regarding this matter:
ISAPI_Rewrite and Coldfusion Rules
With the help of Helicon Support we were able to create the following rules:
RewriteEngine on
RewriteRule on
RewriteBase /
RewriteCond %{QUERY_STRING} ^filter=brand&brand_id=([^&]+)&brand_name=([^&]+)$ [NC]
RewriteRule ^products-search\.cfm$ /search/%1/%2.cfm? [NC,R=301,L]
RewriteCond %{QUERY_STRING} ^$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^search/([^/]+)/([^._]+)\.cfm$ /products-search.cfm?filter=brand&brand_id=$1&brand_name=$2 [NC,L]
RewriteCond %{QUERY_STRING} ^product_id=([^&]+)&product_title=([^&]+)$
RewriteRule ^product-details\.cfm$ /%1/%2.cfm? [NC,R=301,L]
RewriteCond %{QUERY_STRING} ^$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^._]+)\.cfm$ /product-details.cfm?product_id=$1&product_title=$2 [NC,L]
Their help was much appreciated as i am very new to URL Rewriting. The issue i am having now is that the first rule works, it turns the url from:
www.mysite.com/products-search.cfm?filter=brand&brand_id=98&brand_name=HAMANN
to
www.mysite.com/search/98/HAMANN.cfm
Since i am new to this, i do not understand how to get the css/js/images folders to display on this page, what rule or condition do i use for the RewriteBase / part so that everything on the page loads correctly?
The second part of the .htaccess rules do not actually work which change:
product-details.cfm?product_id=1&product_title=This Is A New Product
to
products/This_Is_A_New_Product.cfm
Any help would be greatly appreciated

Many RewriteBase in one .htaccess file?

I have a domain and a wordpress-blog on same server. Now I have a problem (surprise). The wordpress is located on /httpdocs/blog/ and domain is pointing to /httpdocs/ and I'm trying to redirect it to /httpdocs/domain/. But, obvisiously, I have permalinks in Wordpress.
Here's my current .htaccess:
RewriteEngine On
RewriteBase /blog/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
RewriteBase /
RewriteCond %{HTTP_HOST} domain.com
RewriteCond %{REQUEST_URI} !^/domain
RewriteCond %{REQUEST_URI} !^/cgi-bin
RewriteRule ^(.*)$ domain/$1 [L]
But as you already propably assumed, this doesn't work. Wordpress' permalinks affects to /domain/ also, so my images and other urls go wrong.
Any advice? Is it possible to use RewriteBase like this?
No, you can only have one base URL. Just rewrite your rules:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog/. /blog/index.php [L]
RewriteCond %{HTTP_HOST} =example.com
RewriteCond %{REQUEST_URI} !^/domain
RewriteCond %{REQUEST_URI} !^/cgi-bin
RewriteRule ^(.*)$ domain/$1 [L]
I come to this post when I am trying to find solution for a similar problem. It seems that there can be more then one base URL, but the logic does not stop after rewrite. If the URL hit both rewrite base, all the rewrite will be run. Therefore, the strictest rewrite base should be put at the end of the file. For this example, it should be:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} domain.com
RewriteCond %{REQUEST_URI} !^/domain
RewriteCond %{REQUEST_URI} !^/cgi-bin
RewriteRule ^(.*)$ domain/$1 [L]
RewriteBase /blog/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
Noticed that as both rewrite are being run, so if the rewrite contradicts, you will need to fall back to the accepted answer.

.htaccess redirect main domain?

How can I redirect requests to mydomain.tld/somepage.ext to mydomain.tld/mydomain/somepage.ext? I have subdomains like subdomain.mydomain.tld that I don't want to be affected by this.
I can't seem to get it to work right. I'm trying this:
RewriteEngine on
RewriteCond %{HTTP_HOST} mysite.com
RewriteCond %{REQUEST_URI} !^/mysite
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) /mysite/$1
But it isn't redirecting anything at all.
I also want to exclude one or two folders from this rule.
This seems to be working...
RewriteEngine on
RewriteCond %{HTTP_HOST} mysite\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/mysite/ # but I don't see why this line is needed when I've used [L] below
RewriteCond %{REQUEST_URI} !^/ignored_folder/
RewriteRule .* /mysite/$0 [QSA,L]
At least for now.

Resources