please, could you give me an advice, why this .htaccess file cause redirect loop?
I have this url:
www.akomin.cz/ or www.akomin.cz/smth/
and I want to redirect it there:
www.akomin.cz/nedostupny/
But Safari throws me an error, that while trying to open www.akomin.cz/nedostupny/ there were many redirects.
RewriteEngine on
RewriteRule !^/nedostupny/ /nedostupny/ [L,R=302]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.(php|html?|jpg|gif)$
RewriteRule ^(.*)([^/])$ http://%{HTTP_HOST}/$1$2/ [L,R=301]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
Change your first rule to this:
RewriteCond %{THE_REQUEST} !/nedostupny/ [NC]
RewriteRule ^ /nedostupny/ [L,R=302]
You need to use %{THE_REQUEST} because your last rule is changing REQUEST_URI to /index.php hence causing first rule to fire again. THE_REQUEST variable represents original request received by Apache from your browser and it doesn't get overwritten after execution of some rewrite rules.
Make sure to test it after clearing your browser cache.
Related
I force http to https redirection in my .htaccess and it works just fine. But I added RewriteCond %{REQUEST_URI} !^/robots.txt$ rule, not to redirect http://example.com/robots.txt anywhere and just serve the robots.txt file.
The problem is, it does not work and http://example.com/robots.txt gets somehow redirected to http://example.com/index.php url.
My .htaccess is as following:
RewriteEngine On
# HTTPS redirect
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{REQUEST_URI} !^/robots.txt$
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [L]
RewriteRule ^(.+)$ /index.php [L,QSA]
Any suggestions? Thank you.
It redirects because last rule change REQUEST_URI to /index.php. You should be using THE_REQUEST variable in first rule as that doesn't get overwritten after application of rules.
RewriteEngine On
# HTTPS redirect
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{THE_REQUEST} !/robots\.txt [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
Also make sure to test this change in a new browser or test it after completely clearing your browser cache.
I've got the following htaccess file:
RewriteEngine On
RewriteBase /
# Redirect to remove .php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
# Redirect to "page" for dynamic pages
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ page?url=/$1 [L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [R=301,QSA,NC,L]
This allows my custom CMS to use dynamic URLs (http://example.com/some-page, for example) and redirect it to http://example.com/page?url=some-page so that the CMS can render the content. It all works great - until someone adds a URL like http://example.com/something/else. When I spit out the url parameter with: print $_GET['url']; I get /something/else.php/else.
So it seems like the remove .php directive is getting lost and the second parameter is getting duplicated? Thanks for any help.
Have it this way:
Options -MultiViews
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,NE,L]
RewriteCond %{ENV:REDIRECT_STATUS} =200
RewriteRule ^ - [L]
# Redirect to remove .php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^/]+)/?$ $1.php [L]
# Redirect to "page" for dynamic pages
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule !^page\.php$ page.php?url=%{REQUEST_URI} [L,NC,QSA]
Here are changes:
Keep redirect rule before rewrite rules otherwise when www is removed from a URL then your internal URL will be exposed.
Use page.php in target instead of page to avoid another rewrite rule execution.
Use [L] flag in .php adding rule.
Addition of Options -MultiViews
I have a script thanks to Howlin that will rewrite my url, however, I cannot access any other page then, it just gives a 404 error?
The rewrite rewrites localhost/docci.me/profile.php?user=person to localhost/docci.me/person and it works, but as soon as I try to load logout.php, index.php, or any other page, it gives a 404. If I take the .htaccess code out, it works fine but obviously does not rewrite the URL like I need it to.
here is the code:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /docci.me/login/profile\.php\?user=(.*)\ HTTP
RewriteRule ^ /docci.me/login/%2\? [R=301,L]
RewriteCond %{QUERY_STRING} !user=
RewriteRule ^(.*)$ /docci.me/login/profile.php?user=$1 [L]
Place this in /docci.me/.htaccess:
RewriteEngine On
RewriteBase /docci.me/
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} /login/profile\.php\?user=([^&\s]+) [NC]
RewriteRule ^ login/%2? [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^./]+)/?$ login/profile.php?user=$1 [L,QSA]
I need to remove the .php from all the urls on my website, and if someone types in a full url it needs to redirect to the php-extension-free version. Eg:
website.com/about.php -> website.com/about
I need to do this in as SEO friendly a manner as possible, so I'm guessing that would be a 301 redirect so Google and others know that the page has a new location at the new php-extension-free URL. Any dup content would also have to be avoided, so it's important that the page isn't accessible at both the old and new urls, which is what would happen if all I did was a simple:
RewriteRule about about.php [L]
I've seen a number of .htaccess approaches for this, but they all seem to add a trailing slash to the URL. It's important that this doesn't happen too.
Also, I have an explicit HTTPS redirect happening for a couple pages, and I need to make sure I don't create a redirect loop. Here's the code that's currently redirecting those pages to HTTPS.
RewriteCond %{HTTPS} off
RewriteRule ^(quote|quote_2).php$ https://website.com/$1.php [R=301,L,QSA]
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/(quote|quote_2).php
RewriteCond %{REQUEST_URI} !^/(.*)\.(css|png|js|jpe?g|gif|bmp|woff|svg|map)$
RewriteRule ^(.*)$ http://website.com/$1 [R=301,L,QSA]
You can use this code in your root .htaccess:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
RewriteCond %{HTTPS} off
RewriteRule ^(quote|quote_2)/?$ https://%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/(quote|quote_2)
RewriteCond %{REQUEST_URI} !\.(css|png|js|jpe?g|gif|bmp|woff|svg|map)$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]
The below answer can be found here
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Right now i have a url which is like
http://www.example.com/customer/login
i want the URI to always have a ending trail because ill use redirects with ../
and if it doesnt have slash it messes everything up if it has a slash it works fine. I tried to look at some examples online but i couldnt really get anything to work heres my current .htaccess file
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?rt=$1 [QSA]
Anubhava's got the basic answer and I modded him up. You need to send an HTTP redirect to get the browser to request the URL with the / at the end.
To merge with your existing rewrite rules, you should do:
RewriteEngine on
# First check it's not a file
RewriteCond %{REQUEST_FILENAME} !-f
# And it doesn't end in /
RewriteCond %{REQUEST_URI} !.*/$
# Send the redirect. I would do 301 (permanent) here
# the "L" means the rest of the rules are ignored for this request
RewriteRule ^(.*)$ $1/ [L,R=301]
# Now pass thru to your old ruleset URLs the slash-checker didn't catch
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?rt=$1 [QSA]
How about this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [L,R=302]
Try using following rules
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?rt=$1 [QSA]
#following rule checks if url is not ending with / and
#if not then redirected to url which is ending with /
RewriteCond %{REQUEST_URI} !.*/$
RewriteRule ^(.*)$ $1/ [R=302,L]