Excluding a file or directory from .htaccess rewrite - .htaccess

You had a great response that helped me resolve my .htaccess issue on GoDaddy. This was the code you provided:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
QUESTION: How can I exclude a file and/or directory from being rewritten?
Thanks!
David

Assuming you don't want to redirect /ignore.php. Modify above code like this:
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteCond %{REQUEST_URI} !^/ignore\.php$ [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !^/ignore$ [NC]
RewriteRule ^ %{REQUEST_URI}.php [L]

Related

apply slash at the end of the URL with htaccess conditions

I need help here.
I want apply a slash at the end of my urls with htaccess, but unfortunately its not working this way, maybe you guys can help me here?
This is my htaccess:
RewriteEngine on
RewriteBase /
## hide .php extension except in POST method
## To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{REQUEST_METHOD} !POST [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
I tried add this rule, but do not work
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteRule . %{REQUEST_URI}/ [L,R=301]
I appreciate any help here.
Thanks :)

Using htaccess to create pretty links - in my non-empty .htaccess

So right now I'm just trying to convert something like
https://gregorj.org/post?id=26
to
https://gregorj.org/post/26
Everything I've found (also on stackoverflow) is telling me to put this in .htaccess:
RewriteEngine On
RewriteRule ^post/([^/.]+)?$ /post?id=$1 [L]
But it's not working in my case. I'm assuming this might just be because I have a bunch of other stuff in my .htaccess, that I put there to hide .php and .html extensions back when I started the site (which is also why I wrote /post?id=$1 and not /post.php?id=$1 in .htaccess - but I've tried both just in case :).
Here's my .htaccess as it stands today:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# Force HTTPS
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{HTTP_HOST}/$1 [R,L]
# Prevent POST requests from getting redirected
RewriteCond %{REQUEST_METHOD} !POST
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f [NC]
RewriteRule ^ %{REQUEST_URI}.php [L]
# To externally redirect /dir/foo.html to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.html [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.html -f [NC]
RewriteRule ^ %{REQUEST_URI}.html [L]
Any ideas as to what I can do to get the prettier links?
Well, I answered my own question.
If I start out preventing POST requests from getting redirected, then maybe ^post isn't a great way to start a redirect rule...
I changed it to ^article and no problems

.htaccess redirects without extension in url

I'm hiding my file extensions with
RewriteEngine On
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://example.com/folder/$1 [R=301,L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://example.com/folder/$1 [R=301,L]
# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]
When I navigate to this link
http://localhost/website/profile?user=user01&nofollow=1
I'm redirected here
http://example.com/folder/profile?user=user01
I've taken .php out of the url. Why does it redirect me?
Have it this way inside /website/.htaccess:
RewriteEngine On
RewriteBase /website/
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ $1 [R=301,L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php[?\s] [NC]
RewriteRule ^(.+)\.php$ $1 [R=301,L,NC]
# Resolve .php file for extension-less php urls
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^/.]+)$ $1.php [L]
Make sure to clear your browser cache before testing this.
Try using this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=302,L,NE]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f [NC]
RewriteRule ^ %{REQUEST_URI}.php [L]

add www to url and remove .php from url

So I need the urls to look like this
www.mydomain.com/about
here is my current HTACCESS code
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# don't touch /forum URIs
RewriteRule ^forums/ - [L,NC]
# hide .php extension snippet
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
# To remove www header
# RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
# RewriteRule ^(.*)$ http://%1/$1 [L,R=301]
# To add www header
RewriteCond %{HTTP_HOST} ^mydomain.com [NC]
RewriteRule ^(.*)$ www.mydomain.com/$1 [L,R=301]
RewriteCond %{THE_REQUEST} ^GET.*index [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,L]
#404 redirect
ErrorDocument 404 http://www.mydomain.com/
I think the problem is arising in the removing php area. I think its removing the .php and rewriting the url as mydomain.com/about while at the same time, the "add www header" rule is forcing the www in. How can this be fixed?
Options +MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# To add www header
RewriteCond %{HTTP_HOST} ^mydomain.com$
RewriteRule ^(.*) http://www.mydomain.com/$1 [QSA,L,R=301]
# To Remove .php-extension from url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteCond %{REQUEST_URI} ^(.*)\.php$
RewriteRule . %1 [R=301,L]

htaccess with sub site

I have an htaccess file under the main site
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## hide .php or .html extension
# To externally redirect foo.php ot foo.html to foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.(php|html) [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /dir/foo to /dir/foo.html
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [L]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
That works for
http://www.site.com/tenant-referencing/
but I can't get the subsite to work
http://www.site.com/newwebsite/tenant-referencing/
I have the same htaccess under the subsite
Thanks

Resources