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.
Related
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.
I'm trying to get rid of some subdirectories on my website.
I tried this link Remove two subdirectories from url with mod_rewrite but it does not work with my case.
Actually i have this structure : www.mywebsite.com/fr/news/index.html and I want it to look like www.mywebsite.com/index.html.
In my root directory a have a htaccess file with the following:
Options -Indexes +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.html [L]
RewriteCond %{REQUEST_URI} !^/(en|es|pt)/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+fr/([^\s]+) [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_URI} !^/(en|es|pt)/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (?!^fr/)^(.*)/$ /fr/$1 [L,NC]
RewriteCond %{REQUEST_URI} !^/(en|es|pt)/
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.]+\.html(\?[^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)\.html$ http://%{HTTP_HOST}/fr/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !^/(en|es|pt)/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ http://%{HTTP_HOST}/fr/$1/ [R=301,L]
What would the change to do to make it work ?
Thank you in advance for your answers
I have this structure:
www.mywebsite.com/fr/news/index.html
and I want it to look like
www.mywebsite.com/index.html
Without knowing more, I'd suggest placing this in your root folder:
RewriteEngine on
RewriteRule ^[^\/]+\/[^\/]+\/(.*) http://www.mywebsite.com/$1
The regex reads as:
from the start, find one or more characters that aren't a forward slash
then a forward slash
then one or more characters that aren't a forward slash
then a forward slash
then capture all the remaining characters
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]
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.
I have a htaccess which looks like this:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [QSA]
RewriteRule ^$ /naujausios [L,R]
And I'm not sure where to put this:
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
for it to work properly - I tried a few modifications to the keys at the end, without success - I just end up with a 500 response. Thanks for your help!
I had conflicts wherever I put it, but the solution was to put the www bit rewrite at the very top just after RewriteEngine on