Issue changing a word in a URL using mod_rewrite - .htaccess

I'm trying to change the word "portfolio" in my URLs to "portfolio/project" and inadvertently created a redirect loop. Would appreciate any help in pointing me in the right direction.
Example:
http://www.example.com/portfolio/interactive/abc/ to
http://www.example.com/portfolio/project/interactive/abc/
Current htaccess (last two lines relates to issue):
redirect 301 "/sitemap.xml" http://www.example.com/sitemap.php
RewriteEngine On
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/sitemap.php
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{REQUEST_URI} /.*portfolio.*$ [NC]
RewriteRule ^(.*)portfolio(.*)$ /$1portfolio/project$2 [R=301,L]

Your problem is that your regex also matches your target, so after the redirect, the URI matches the same rule and gets redirected again (you may have noticed that there's a bunch of /project/project/project/project/project/project/project in the URI)
Add an exclusion condition:
RewriteCond %{REQUEST_URI} !portfolio/project
RewriteCond %{REQUEST_URI} /.*portfolio.*$ [NC]
RewriteRule ^(.*)portfolio(.*)$ /$1portfolio/project$2 [R=301,L]

Related

Htaccess - Exclude a word from existing rewrites

I have the following in my htaccess file:
# drop tags
#RewriteCond %{THE_REQUEST} (.*)designs/(.*)/?tag=shirts [NC]
#RewriteRule .* /designs/%2/ [R=301,L]
RewriteCond %{THE_REQUEST} (.*)designs/([^?]+)\?tag=[^&]* [NC]
RewriteRule .* /designs/%2? [R=301,L]
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+product/search/\?tag=([^\s&]+) [NC]
RewriteRule ^ /%1/? [R=301,L,NE]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/designs/ [NC]
RewriteRule ^([^/]+)/?$ product/search/?tag=$1 [L,QSA]
RewriteCond %{QUERY_STRING} ^search= [NC]
RewriteRule ^designs/.*$ /$0? [L,R=301,NC]
RewriteCond %{QUERY_STRING} ^mfp= [NC]
RewriteRule ^designs/ %{REQUEST_URI}? [L,NC,R=301,NE]
My theme supports a blog but after enabling it (it defaults to example.com/blog/), when I click on the blog link in my menu, it takes me to a page that says "There is no product that matches the search criteria". When I remove the htaccess rules listed above, the blog page (which contains the article listings) works fine so it is definitely that.
How can I exclude the word "blog" from the htaccess rules so this issue disappears?
I managed to fix it after a couple hours of tinkering. The solution was:
# external redirect from actual URL to pretty one
RewriteCond %{REQUEST_URI} !^/blog($|/)$
RewriteCond %{THE_REQUEST} \s/+product/search/\?tag=([^\s&]+) [NC]
RewriteRule ^ /%1/? [R=301,L,NE]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_URI} !^/blog($|/)$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/designs/ [NC]
RewriteRule ^([^/]+)/?$ product/search/?tag=$1 [L,QSA]
This solution also handles /blog (without the trailing /).
Hope that helps someone in the future.

Using backreference after RewriteCond in htaccess

I have:
RewriteCond %{HTTP_HOST} ^my.domain.com$ [NC]
RewriteRule ^(.*)$ index.php?q=search&keyword=$1
Input:
my.domain.com/foo_bar
I want:
index.php?q=search&keyword=foo_bar
But in fact:
index.php?q=search&keyword=index.php
I don't understand why. Please help me!
Your rewrite rule is actually rewriting twice, once for /foo_bar and second time for index.php as .* matches anything.
You just need to add 2 conditions to stop rewrite for files and directories:
# handle landing page
RewriteCond %{HTTP_HOST} ^my\.domain\.com$ [NC]
RewriteRule ^/?$ index.php?q=search [L,QSA]
# handle /foo_bar
RewriteCond %{HTTP_HOST} ^my\.domain\.com$ [NC]
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?q=search&keyword=$1 [L,QSA]

.htaccess not working properly

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.

.htaccess redirect from 1 url (no matter what file/folder) to another url

I have a website http://rochesterwaterskishow.com which they've recently changed their name so they want to update their url to http://skidox.com. I'm trying to redirect any page from rochesterwaterskishow.com to skidox.com/site/index.
I have this line of code which redirects http://rochesterwaterskishow.com to http://skidox.com, but if I go to something like http://rochesterwaterskishow.com/test, it doesn't redirect to http://skidox.com.
RewriteRule ^$ http://skidox.com/site/index [R=301,L]
How can I make it a catch all so anything rochesterwaterskishow.com/* gets redirected to skidox.com/site/index?
UPDATE: Full .htaccess file
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
RewriteRule ^$ http://skidox.com/site/index [R=301,L]
That's because the search pattern ^$ will only match a URI path of "/". You need to pick up the request in a match variable, for example:
RewriteCond %{HTTP_HOST} rochesterwaterskishow
RewriteRule ^.* http://skidox.com/site/index/$0 [R=301,L]
I am assuming that you are using SEO optimised-style URIs for the new site. If you want to simply redirect everything to the index page without any context, then you still need a pattern that matches:
RewriteCond %{HTTP_HOST} rochesterwaterskishow
RewriteRule ^ http://skidox.com/site/index [R=301,L]
Update following post of full htaccess
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} rochesterwaterskishow
RewriteRule ^.* http://skidox.com/$0 [R=301,L]
RewriteCond $0 ^(index\.php$|robots\.txt$|resources)
RewriteRule ^.* - [S=1]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
RewriteRule ^$ http://skidox.com/site/index/$1 [R=301,L]

.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