How can I redirect URLs which has extension like .css or .js but not .php to specific folder.
I mean all the URLs which has extension but their extension is not php should be redirect to specific folder.
I try to do it like this but it does not work:
RewriteCond %{REQUEST_URI} \.(.{2,4}!php)$
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*)\.(.{2,4}!php)$ folder/$1.$2 [L]
Well I have found the solution:
RewriteCond %{REQUEST_URI} \.(.{2,4})$
RewriteCond %{REQUEST_URI} !\.(php)$
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*)$ $1 [L]
First I check if the extension is in desire length, then I check it is not end with .php
Your regex isn't right, you want simply !\.php$:
RewriteCond %{REQUEST_URI} \.\w{1,4}$
RewriteCond %{REQUEST_URI} !\.php$ [NC]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*)$ folder/$1 [L]
Related
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 included the code for making the URLs work with and without .php. I included the below written code
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Now the url works with and without .php. For e.g. if we put http://www.test.com/test.php or http://www.test.com/test both works. But the problem is folders doesn't load. For e.g. http://www.test.com/admin doesn't load. not found error is shown. Admin is a folder. Any idea?
Try this code for .php extension removal instead:
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^GET\s.+\.php [NC]
RewriteRule ^(.+)\.php$ /$1 [R=301,L,NC]
# To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_URI} !\.php$ [NC]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule . %{REQUEST_URI}.php [L]
Make sure to comment out your existing code
Try below :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [NC,L]
this will also work with a directory path.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]
just try this...I think its working
I want to redirect to another site (storage), if client requests *.pdf or *.cdr files.
The problem is that, with the below htaccess, cdr downloading works, but not pdf.
If a change the rule order (cdr first, pdf second), then the pdf works, but not the cdr.
I've tried [OR], other regex to catch both extensions in one pattern. This is the only one that works (half-works).
The files (pdf or cdr) to which the redirection points exist (I checked through FTP).
I currently have in the root .htaccess:
#<snip>
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
# catch site.ro/some_file.pdf
RewriteCond %{REQUEST_URI} (.+pdf)$ [NC]
RewriteRule ^(.*)$ http://docs.site.com/$1 [L,R]
# catch site.ro/some_file.cdr
RewriteCond %{REQUEST_URI} (.+cdr)$ [NC]
RewriteRule ^(.*)$ http://docs.site.com/$1 [L,R]
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
This is what I would do:
RewriteCond %{REQUEST_FILENAME} !-d # If not an existing directory
RewriteCond %{REQUEST_FILENAME} !-f # If not an existing file
RewriteCond %{REQUEST_URI} \.(pdf|cdr)$ [NC] # If filename ends with .pdf or .cdr
RewriteRule ^(.*)$ http://docs.site.com/$1 [L,R] # Redirect the user to the other site
RewriteRule ^.*$ index.php [NC,L] # Redirect all other requests to index.php
Hope this helps you
I want that every user has a profile-url like this:
www.example.com/username
I have created this htaccess:
RewriteEngine on
RewriteRule ([^/]+) profile.php?username=$1 [L]
but I get error with the css and js files.
what I have to write in the htaccess ?
You should in general exclude all real files and directories, as this will handle css/js/images/whatever else you want to serve:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ profile.php?username=$1 [L]
Exclue js and css with RewriteCond.I think this should do:
RewriteEngine On
RewriteCond %{REQUEST_URI} ! \.(js|css)$ [NC]
RewriteRule ([^/]+) profile.php?username=$1 [L]
If you have images as well, add their extensions
RewriteCond %{REQUEST_URI} ! \.(js|css|jpg|gif|ico|png|whatever)$ [NC]
I am trying to include certain file extensions from being rewritten fi they dont exit.
(Image Types: jpg|gif|png)
Currenty I have:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/$1 -d
RewriteRule (.*) - [S=1]
RewriteRule (.*) /index.php?id=$1 [L]
I would need to skip this is the request url ends in: .gif/.jpg/.png
Been looking everywhere.. but can't find out how to do it. Thanks!
This should do it:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_URI} !\.(gif|jpg|png)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /index.php?id=$1 [L]
This basically says, filename doesn't end with gif jpg or png, and it isn't a file, and isn't a directory, then send it to index.php
I should have thought an additional RewriteCond on that list would suffice.
RewriteCond %{REQUEST_URI} !(\.jpg|\.gif|.png)$
Not forgetting the [OR] where necessary.