Htaccess - Exclude a word from existing rewrites - .htaccess

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.

Related

htaccess no-trailing-slash policy: non existing urls (404) should not redirect

If I open a non existing url on my site like www.domain.de/abcde/ (with trailing slash) the url redirects to www.domain.de/abcde (without trailing slash) and opens the 404 site after.
But the 404 site should come directly when the url does not exist (without redirecting for the non-trailing-slash policy).
What do I need to add to my htaccess? Thank you!
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [R=301,L]
------
starkeens suggestion does only work on a clean htaccess, but I got some more code. It does not work together with the other lines. All the code:
RewriteEngine on
RewriteBase /
# redirect urls without www. to url with www.
RewriteCond %{HTTP_HOST} ^domain\.de$
RewriteRule ^(.*)$ http://www.domain.de/$1 [L,R=301]
# hide the suffix of urls
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.+)\.html\ HTTP/
RewriteRule (.*).html$ /$1 [R=301,L]
# start redirect everything to the subfolder
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.de [NC]
# except these folders:
RewriteCond %{REQUEST_URI} !^/downloads [NC]
RewriteCond %{REQUEST_URI} !^/cms [NC]
RewriteCond %{REQUEST_URI} !^/vorschau [NC]
RewriteCond %{REQUEST_URI} !^/domain/.*$
RewriteRule ^(.*)$ /subfolder/$1
# end redirect everything to the subfolder
# no-trailing-slash policy
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [R=301,L]
Any new suggestions? :)
TRY :
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ - [R=404,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [R=301,L]
Finally i got a solution that seems to work.. maybe anyone got the same issue and needs the code. Comments for optimization are welcome!
# trailing Slashes
# to enforce a no-trailing-slash policy with subfolder
# is the request for a non-existent file?
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# if so, skip the next RewriteRule
RewriteRule .? - [S=1]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [R=301,L]
Now only existing files get the redirect of my no-trailing-slash policy. Non existing files aren't redirected and occur a 404 page directly, as wished.

Using .htaccess to route friendly url

I am trying to route an url which is displayed as follows www.example.com/profile/1 to the following page with parameters www.example.com/profile?user=1. I have the following RewriteRule in my .htaccess, yet the routing is not working. Can someone tell me what I am doing wrong?
RewriteEngineOn
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/profile/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.+) /profile?user=$1 [L,QSA]
I have tried switching up the last RewriteRule to different URL's, but so far no luck. I am probably overseeing a small problem which someone can hopefully help me out with.
You need to allow for profile/\w+ as regex pattern to match /profile/123:
Options -MultiViews
RewriteEngineOn
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^profile/(\w+)/?$ /profile?user=$1 [L,QSA,NC]

htaccess RewriteRule with parameters to specific location

I have a page which has a link as:
http://www.example.com/listings.php?packageType=luxury-travel
Which I'd like to send to:
http://www.example.com/luxury-travel
And also:
http://www.example.com/listings.php?packageType=luxury-travel&packageTypeSub=remote-luxury-retreats
To go to:
http://www.example.com/luxury-travel/remote-luxury-retreats
I have used:
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)$ /listings.php?packageType=$1&packageTypeSub=$2 [L]
Which ends up affecting other pages which I have such as:
http://www.example.com/about-us
I have also tried placing:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Before. Also tried playing with:
RewriteCond %{QUERY_STRING} ^packageType=([^&]+) [NC,OR]
RewriteCond %{QUERY_STRING} &packageType=([^&]+) [NC]
RewriteRule ^([^/]*)$ /listings.php?packageType=$1 [L]
Which just give me a 404 error on both links. Anyone have any ideas? Would have thought this would have been a simple one!
In your .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([^\.*]+)/([^\.*]+)/?$ listings.php?packageType=$1&packageTypeSub=$2 [L,QSA]
RewriteRule ^/?([^\.*]+)/?$ listings.php?packageType=$1 [L,QSA]
Try this:
RewriteEngine on
RewriteCond %{THE_REQUEST} \ /+listings\.php\? [NC]
RewriteCond %{QUERY_STRING} (?:^|&)packageType=([^&]+)&packageTypeSub=([^&\s]+) [NC]
RewriteRule ^ /%1/%2? [R=301,NC,L]
RewriteRule ^([^/]+)/([^/]+)?$ /listings.php?packageType=$1&packageTypeSub=$2 [L]
Note that if only packageType is available, the rules would redirect to http://www.example.com/luxury-travel/ (note the / at the end) to help differentiate it from links like http://www.example.com/about-us.
If that's not an option, then you would have add rules that specifically provide page/link names.
I have edited and removed some of the development code as it wasn't working.
After a lot of headscratching I finally found a solution which works:
# Rewrite specific pages - listings
# Allow packageType
RewriteCond %{THE_REQUEST} \ /+listings\.php\? [NC]
RewriteCond %{QUERY_STRING} (?:^|&)packageType=([^&]+) [NC]
RewriteCond %{QUERY_STRING} !&packageTypeSub=([^&]+)
RewriteRule ^ /%1/? [R=301,NC,L]
# Allow packageTypeSub
RewriteCond %{THE_REQUEST} \ /+listings\.php\? [NC]
RewriteCond %{QUERY_STRING} (?:^|&)packageType=([^&]+)&packageTypeSub=([^&\s]+) [NC]
RewriteRule ^ /%1/%2? [R=301,NC,L]
# Ignore the images folder allowing images to display
RewriteCond %{REQUEST_URI} !(upload-images) [NC]
RewriteRule ^([^/]+)/([^/]+)?$ /listings.php?packageType=$1&packageTypeSub=$2 [L]
The main issue was the upload-images folder which contains all the images for the site packages was being blocked. I managed to exclude this folder from the rewrite rules and it now seems to be working. Thanks all for your help. Hope this helps someone else in future.

.htaccess URL structure with variables stops working w/ pagination

I have a problem with htaccess on one of these 2 pages: index.php & search.php
Both pages are functional & dependent on variables being sent to them via "GET" method.
SEARCH Example -
search.php?tag=KEYPHRASE
Showing as: site.com/tag/KEYPHRASE
# HTACCESS:
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^tag/(.*)/?$ /search.php?tag=$1 [NC,L]
# internal forward
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^tag/(.+?)/?$ /search.php?tag=$1 [L,QSA,NC]
# external rewrite
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+search\.php\?tag=([^\s]+) [NC]
RewriteRule ^ /tag/%1? [R=302,L]
INDEX Example -
index.php?id=ID&title=TITLE
Showing as: site.com/cover/TITLE-ID
# HTACCESS:
RewriteRule ^cover/(.*)-(.*)/?$ index.php?id=$2&title=$1 [NC,L]
# internal forward FOR SINGLE PAGE (index)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^cover/(.+?)-(.+?)/?$ /index.php?id=$2&title=$1 [L,QSA,NC]
# external rewrite
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?id=([^\s]+)&title=([^\s]+) [NC]
RewriteRule ^ /cover/%2-%1? [R=302,L]
Both pages also have pagination, my problem is, it only works for search.php
SEARCH page w/ pagination:
site.com/tag/KEYPHRASE&page=# WORKS! Correct paginated results being returned.
INDEX page w/ pagination:
site.com/title&page=#-ID Inserts &page=# in between the title & ID in the URL, but shows results.
The only difference I see between these 2 are the number of variables being passed on, otherwise they are being rewritten exactly the same.
I need help with making my .htaccess structure work seamlessly w/ pagination query strings for my index.php page URL.
You need to use QSA flag in your internal rewrite rules and rearrange your rules.
Keep your .htaccess like this:
RewriteEngine On
# external rewrite
RewriteCond %{THE_REQUEST} \s/+index\.php\?id=([^&\s]+)&title=([^&\s]+)&page=([^&\s]+) [NC]
RewriteRule ^ /cover/%2-%1/%3? [R=302,L]
RewriteCond %{THE_REQUEST} \s/+index\.php\?id=([^&\s]+)&title=([^&\s]+)\s [NC]
RewriteRule ^ /cover/%2-%1? [R=302,L]
# external rewrite
RewriteCond %{THE_REQUEST} \s/+search\.php\?tag=([^&\s]+)&page=([^&\s]+) [NC]
RewriteRule ^ /tag/%1/%2? [R=302,L]
RewriteCond %{THE_REQUEST} \s/+search\.php\?tag=([^&\s]+)\s [NC]
RewriteRule ^ /tag/%1? [R=302,L]
# internal forward FOR SINGLE PAGE (index)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^cover/([^-]+)-([^/]+)/([0-9]+)/?$ /index.php?id=$2&title=$1&page=$3 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^cover/([^-]+)-([^/]+)/?$ /index.php?id=$2&title=$1 [L,QSA,NC]
# internal forward
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^tag/([^/]+)/?$ /search.php?tag=$1 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^tag/([^/]+)/([0-9]+)/?$ /search.php?tag=$1&page=$2 [L,QSA,NC]

Issue changing a word in a URL using mod_rewrite

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]

Resources