htaccess rule question - .htaccess

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

Related

Htaccess Redirect issues in joomla project

I have a redirect issue.
I am trying to redirect from http://www.project.com/index.php to http://www.project.com
I tried the following, but it goes one step back to any index.php like that.
RewriteCond %{THE_REQUEST} ^.*/index\.php
RewriteRule ^(.*)index.php$ /$1 [R=301,L]
To remove index.php use:
RewriteBase /
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
However, you can also use the below as an alternative (but it will require you to enable mod_rewrite for it to work:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]

Multiple rewrite rules and conditions in htaccess file

In my .htaccess file i have already some rewrite conditions and rules, and its working normal.
Now i need to add "http to https redirect" to my htaccess file.
Here is my old .htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteRule ^([a-z0-9_-]+)$ pages/$1.php [L]
RewriteRule ^([a-z0-9_-]+)/([-a-zA-Z0-9_]+)$ pages/$1/$2.php [L]
RewriteRule ^([a-z0-9_-]+)/([-a-zA-Z0-9_]+)/([-a-zA-Z0-9_]+)$ pages/$1/$2/$3.php [L]
I attempted to add below code to file but it doesnt work properly.
For example if i write the url direct to browser its work (only with www). But if click my any backlinks or google search result links it doesnt work.
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}/$1 [NC,R=301,L]
Where do i wrong? Where should i put the code? Any help would be great. Thanks.
your htaccess could look like this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.yoursite\.com [NC]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteRule ^([a-z0-9_-]+)$ pages/$1.php [L]
RewriteRule ^([a-z0-9_-]+)/([-a-zA-Z0-9_]+)$ pages/$1/$2.php [L]
RewriteRule ^([a-z0-9_-]+)/([-a-zA-Z0-9_]+)/([-a-zA-Z0-9_]+)$ pages/$1/$2/$3.php [L]
At least, it works for me, in my own implementation
You can use the following htaccess :
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}/$1 [NC,R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+)/?$ $1.php [L]
Clear your browser's cache before testing this

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 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]

Can someone give me the code for a specific .htaccess?

I tried to make my own .htaccess file with a few thing in it, but I'a not very familiar with it. I googled a lot but at the end I only managed to mess things up.
So, can someone give me working .htaccess code snippet?
I want it to do the following:
redirect from non-www to www
redirect index.php to / in all directories
remove ".php" from all files in all directories
redirect URL's like product.php?detail=Phone to product/detail/Phone
(also manage the trailing slash problem)
Hope someone can help.
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
#non-www to www
RewriteCond %{HTTP_HOST} ^nevca.getfreehosting.co.uk [NC]
RewriteRule ^(.*)$ http://example.co.uk/$1 [R=301,L]
# index.php to /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)index\.php$ /$1 [R=301,L]
#index/menu/123/ to index.php?menu=123
RewriteRule ^index/([^/]+)/?$ /index.php?menu=$1 [L]
#index.php?menu=123 to index/menu/123/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index2\.php\?menu=([^&]+)\ HTTP/
RewriteRule ^index\.php$ http://example.co.uk/index/%1/? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php
# Forces a trailing slash to be added
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
Something like this, just the last 2 rules don't work...
And I also want the .php to be removed only when request is not like ".php?..."
There are some of these thing in the html5 boilerplate htaccess, you should take a look and use the part you want:
http://html5boilerplate.com/

Resources