this is my current .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
RewriteCond %{REQUEST_URI} .*jpg$|.*gif$|.*png$|.*css$|.*js$ [NC]
RewriteRule (^.*) http://d14t2ycfqndlt4.cloudfront.net/$1 [R=301,
the last 2 lines dont work, althouh when i remove the first cond above, id does work...
what is the problem? thanks!
Something like this should work:
RewriteEngine On
RewriteCond %{REQUEST_URI} (.*)\.(jpg|gif|png|css|js)$ [NC]
RewriteRule (.*) http://d14t2ycfqndlt4.cloudfront.net/$1 [R=301,...]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule (.*) - [NC,L]
RewriteRule (.*) index.php [NC,L]
Note the regular expressions are a bit easier to understand.
This is your corrected and cleaned up .htaccess code:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule \.(?:jpg|gif|png|css|js)$ http://d14t2ycfqndlt4.cloudfront.net%{REQUEST_URI} [R=301,L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
Related
I have 2 different rewrites in my .htaccess file that I need to combine into 1.
The first redirects all requests to the https://www version of the website.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^w3studios\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.mywebsite\.com$
RewriteRule ^(.*)$ "https\:\/\/www\.mywebsite\.com\/$1" [R=301,L]
</IfModule>
The second gets all the post data and assigns to a variable.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
How can I combine these to both work?
Thank-you
You can use this combined set of rules in your site:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,NE,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
I created a .htaccess with the following goals:
1: Remove .php and force add trailing slash
2: Make http://website.com/sticker/fruit/apple = sticker.php?category=fruit&alt=apple
That works, but whenever I type something that does not exist, for example:
http://website.com/sdgsdg/ OR
http://website.com/dssdg/sdgsg/zkzzv/
I get redirect errors.
Here is my .htacess:
Options +FollowSymlinks
RewriteEngine on
# Remove .php extension
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php/$ http://website.com/$1/ [L,R=301]
# Remove .php extension
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ http://website.com/$1/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(.+)/$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^(.*)/$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/])$ http://website.com/$1/ [L,R=301]
## If the request is for a valid directory
RewriteCond %{REQUEST_FILENAME} -d [OR]
## If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f [OR]
## If the request is for a valid link
RewriteCond %{REQUEST_FILENAME} -l
## don't do anything
RewriteRule ^ - [L]
RewriteRule ^sticker/([^/.]+)/([^/.]+)/$ sticker.php?category=$1&alt=$2 [NC,L]
Can anyone spot whats causing the redirect loops?
Thank you in advance.
I found a solution that worked for me.
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(.+)/$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^(.*)/$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(.+)/$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]
RewriteRule ^sticker/([^/.]+)/([^/.]+)/$ sticker.php?category=$1&alt=$2 [NC,L]
I wanted to create an htaccess file to remove the .php extension and change course.php?id=1 to course/1. But that does not seem to work out. Can any one correct my htaccess?(I am quite new to writing htaccess files)
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
# If request is for a file that does actually exist, don't rewrite.
RewriteCond %{REQUEST_FILENAME} !-f
# If request is for a directory that does actually exist, don't rewrite.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/course/(.*)$ /course.php?id=$1 [NC]
Options -Indexes
Keep it this way:
Options -Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# add a trailing slash
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule ^(.+)$ $1/ [R=301,L]
RewriteRule ^course/([^/]+)/?$ course.php?id=$1 [NC,L,QSA]
RewriteRule ^([^/]+)/$ $1.php [L]
RewriteRule ^([^/]+)/([^/]+)/$ $1/$2.php [L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)$ product.php?ref=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)$ /category.php?sub=$1 [L]
The values sent with variable sub conflicts with values from ref
product.php?ref=$1 [L]
category.php?sub=$1 [L]
My advice for any kind of rewrite rules that intend some form of semantic URLs, that is:
/home
instead of
index.php?page=home
Make one rule that captures all URLs, like:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ handler.php?__url=$1 [QSA]
Then have handler.php figure out what you want to do.
You know your categories and products, so if the URL is /[product|category] then handle it.
With little bit of change following code should work:
Options +FollowSymLinks -MultiViews
RewriteEngine On
# don't do anything for a valid file or directory
RewriteCond %{ENV:REDIRECT_STATUS} !^$ [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# Product handler /i/test
RewriteRule ^i/([^/]+)/?$ /product.php?ref=$1 [L,NC,QSA]
# PHP handler
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+)$ /$1.php [L]
# category handler
RewriteRule ^([^/]+)/?$ /category.php?sub=$1 [L,QSA]
Here is my code:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond application/public/%{REQUEST_FILENAME} -f
RewriteRule (.*) application/public/$1 [L]
I want to make expression:
If file {REQUEST_FILENAME} doesn't exist AND if file application/public/%{REQUEST_FILENAME} exists do rewrite
Example results:
/style.css = /application/public/style.css
/gfx/logo.png = /application/public/gfx/logo.png
/index.php = /index.php
/Welcome = /index.php?input=Welcome (it's working)
What is proper code for this expression? I think i need to change line:
RewriteCond application/public/%{REQUEST_FILENAME} -f
But i have no bloody idea how...
Full .htaccess listing:
RewriteEngine on
RewriteRule style,(.+).css tmp/merged/css,$1 [L,QSA]
RewriteRule script,(.+).js tmp/merged/js,$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond application/public/%{REQUEST_FILENAME} -f
RewriteRule (.*) application/public/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) index.php?input=$1 [L,QSA]
(sorry for my english)
i've found small workaround, but it's not elegant solution :(
RewriteEngine on
# Access to files in ./application/public
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) application/public/$1 [DPI]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule application/public/(.*) $1 [DPI]
# Main rewrite
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) index.php?input=$1 [L,QSA,DPI]