The following regex works perfectly except for under the condition when the user goes to examplesite1.com/site1. When this happens the page fails because of multiple redirects in which it continues to add the request=site1 query to the URL. I thought that adding RewriteCond %{ENV:REDIRECT_STATUS} ^$ would fix this issue however that seemed to not be the case.
This setup is for a multi-domain website. There are three folders site1, site2, and shared. There are also rewrite rules for site2 and shared that are not shown as they do not effect this issue.
# if the resource exists in site1 return it to the user
RewriteCond %{HTTP_HOST} examplesite1.com$
RewriteCond %{DOCUMENT_ROOT}/site1/%{REQUEST_URI} -f
RewriteRule ^(.*)$ /site1/$1 [QSA,L]
# no file found, send URI to CMS
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?request=$1 [QSA,L]
complete .htaccess file:
AddDefaultCharset UTF-8
# Disallow browsing file directories
Options -Indexes
RewriteEngine on
RewriteBase /
#remove the trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} (.*)/$
RewriteRule ^(.+)/$ /$1 [R=301,L]
RewriteCond %{HTTP_HOST} examplesite1.com$
RewriteCond %{DOCUMENT_ROOT}/site1/%{REQUEST_URI} -f
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.*)$ /site1/$1 [QSA,L]
RewriteCond %{HTTP_HOST} examplesite2.com$
RewriteCond %{DOCUMENT_ROOT}/site2/%{REQUEST_URI} -f
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.*)$ /site2/$1 [QSA,L]
RewriteCond %{DOCUMENT_ROOT}/shared/%{REQUEST_URI} -f
RewriteRule ^(.*)$ /shared/$1 [QSA,L]
# resources not to be public are sent to CMS
RewriteCond %{REQUEST_FILENAME} (\.inc\.|\.tpl$)
RewriteRule ^(.*)$ /index.php?request=$1 [QSA,L]
# no resource found, send URI to CMS
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.*)$ /index.php?request=$1 [QSA,L]
Try adding an extra condition:
RewriteCond %{HTTP_HOST} examplesite1.com$
RewriteCond %{REQUEST_URI} !^/site1/
RewriteCond %{DOCUMENT_ROOT}/site1/%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/site1/%{REQUEST_URI} -d
RewriteRule ^(.*)$ /site1/$1 [QSA,L]
What looks like may be happening here is that you need to prevent a redirect from happening after one of your other rules get applied. Try adding this right below RewriteBase /:
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
Related
When user enters url like
http://example.com/app/abcd123/
I want to show hime page from
http://example.com/app/index.php?param=abcd123
Without changing URL in browser.
I put .htaccess file inside app folder with code
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/index.php [NC]
RewriteCond %{QUERY_STRING} ^param=(.*)
RewriteRule (.*) http://example.com/app/%1? [R=301,L]
You can use this code in /app/.htaccess:
RewriteEngine on
RewriteBase /app/
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /index\.php\?param=([^\s&]+) [NC]
RewriteRule ^ %1? [R=302,L,NE]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)/?$ index.php?param=$1 [L,QSA]
Try this .htaccess code. It should help you.
Make sure the rewrite base should be the form the root to your present directory.
RewriteBase /app/
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^(.*)$ index.php?param=$1 [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ index.php [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} -s
RewriteRule ^(.*)$ index.php [QSA,NC,L]
In this, if the user enters like http://example.com/app/qwerty the call will be processed like http://example.com/app/index.php?params=qwerty
This should be working, I tested it. Let me know if you face any troubles.
RewriteEngine On
RewriteBase /app
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php?param=$1 [L]
Check it there: http://htaccess.mwl.be/ or other online htaccess test service
I want directory www.example.com/core to be translated into string, instead of just dissalowing access to it. is that possible?
UPDATE (STILL NO LUCK):
My current .htaccess
#Options -Multiviews
RewriteEngine On
#Remove the comments below to enable enforcing HTTPS
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Handle Front Controller...
RewriteBase /public
RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^(.*)$ launcher.php?urls=$1 [QSA,L]
RewriteRule . launcher.php [L]
Here are some scenarios:
I write a url www.example.com/some-dir-that-does-not-exist and it works fine.
I write a url www.example.com/url-that-DOES-exist and the browser redirects it to www.example.com/url-that-DOES-exist/?url=url-that-DOES-exist
That is due to mod_dir module adding a trailing slash in front of real directories and making a 301 redirect after your rewrite rule.
To fix have it like this:
DirectorySlash Off
RewriteEngine On
RewriteBase /public/
RewriteCond %{THE_REQUEST} /launcher\.php [NC]
RewriteRule ^ - [F]
#Remove the comments below to enable enforcing HTTPS
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# add a trailing slash to directories
RewriteCond %{DOCUMENT_ROOT}/$1 -d
RewriteRule ^(.*?[^/])$ %{REQUEST_URI}/ [L]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*?)/?$ launcher.php?urls=$1 [QSA,L]
Your code will become:
#Options -Multiviews
RewriteEngine On
#Remove the comments below to enable enforcing HTTPS
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteBase /public
# Treat existing /core directory as non-existing (handled by launcher.php)
RewriteCond %{REQUEST_URI} ^/core(/.*)?$
RewriteRule . launcher.php [L]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^(.*)$ launcher.php?urls=$1 [QSA,L]
RewriteRule . launcher.php [L]
I've been trying to delete the .php extension and force trailing slashes in the end of the URLs.
I've found an answer to do it exactly what I need at (.htaccess trouble with hiding file extension and forcing trailing slash) but it does not work, I believe it's due to how I am forcing my website to change the document root.
As I do not have access to Virtual Hosts I must change my document root using htaccess, and I have accomplished it by applying the following code..
RewriteCond %{HTTP_HOST} ^site.co$ [NC]
RewriteRule ^(.*)$ http://www.site.co/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !webroot/
RewriteRule (.*) /webroot/$1 [L]
The only working solution that I've been able to apply to hide the .php extension is with the code below.
removing .php extension(only working solution)
RewriteCond %{THE_REQUEST} ^\w+\ /(.*)\.php(\?.*)?\ HTTP/
RewriteRule ^ http://%{HTTP_HOST}/%1 [R=301]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .* $0.php
This code is in the thread that I have linked above, but it does not work on my website as I believe it's due to the document root.
RewriteEngine on
# Forces a trailing slash to be added
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?[^/])$ /$1/ [R=301,L]
# .php ext hiding
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ /$1.php [L]
Try these modified rules with different ordering:
RewriteEngine on
# add www before hostname
RewriteCond %{HTTP_HOST} ^site\.co$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=302,L,NE]
# if on article page, get slugs and make into friendly url
RewriteCond %{THE_REQUEST} \s/article\.php\?article_uid=([^&]+)&article_title=([^&\ ]+)
RewriteRule ^ /article/%1/%2/? [L,R=302,NE]
# if page with .php is requested then remove the extension
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1/ [R=302,L,NE]
# Force a trailing slash to be added
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s/+([^.]+?[^/.])[\s?] [NC]
RewriteRule ^ /%1/ [R=302,L]
# allow page direction to change the slugs into friendly seo URL
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (?:^|/)article/([^/]+)/([^/]+)/?$ /webroot/article.php?article_uid=$1&article_title=$2 [L,QSA,NC]
# silently rewrite to webroot
RewriteCond %{REQUEST_URI} !/webroot/ [NC]
RewriteRule ^ /webroot%{REQUEST_URI} [L]
# .php ext hiding
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
My .htaccess file is creating a redirect loop. I have searched online and tried lots of varieties including many from this site but still can't get it to work. The purpose of it is to remove the .html from the end of the URL.
Here is my code:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME}/ -d
RewriteCond %{SCRIPT_FILENAME}.html !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.+)\.html$ /$1 [R=301,L]
RewriteCond %{SCRIPT_FILENAME}.html -f
RewriteRule [^/]$ %{REQUEST_URI}.html [QSA,L]
Replace all of your code with this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## hide .html extension
# To externally redirect /dir/foo.html to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.+?)\.html?[/?\s] [NC]
RewriteRule ^ %1? [NE,R=301,L]
# To internally forward /dir/foo to /dir/foo.html
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^(.+?)/?$ /$1.html [L]
## append trailing slash if needed
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]
I have a shared hosting with multiple domain hosted on it. In the root folder of my hosting exists a .htaccess (say is htaccess1). Code in ht1 is:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?mydomain.com$ [NC]
RewriteCond %{REQUEST_URI} !^/mydomain/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /mydomain/$1
RewriteCond %{HTTP_HOST} ^(www.)?mydomain.com$ [NC]
RewriteRule ^(/)?$ mydomain/index.php [L]
These rules are there so that only urls with www.mydomain.com use all files of mydomain folder. I am new to url rewriting so I don't understand the meaning of what each line does. Another .htaccess (say htaccess2) file exists in mydomain folder. Code is htaccess2 is:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+)?/$ try.php?id=$1
RewriteRule ^([0-9]+)?$ try.php?id=$1
RewriteRule ^post/([0-9]+)?/$ post.php?id=$1
RewriteRule ^post/([0-9]+)?$ post.php?id=$1
In this file, line 6,7 works fine and redirect requests with numeric parameters to try.php but lines 8,9 doesn't work and gives a 404 page not found error, exact error string is :
The requested URL /mydomain/post/1233445 was not found on this server.
I doubt that some effect of htaccess1 is creating this problem as I tried many variations of regex in line 8,9. Please help.
Main .htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/mydomain/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /mydomain/$1 [L]
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$ [NC]
RewriteRule ^$ /mydomain/index.php [L]
Changes:
Added RewriteBase at the top.
Added L flag to mark it last rule.
Added required options.
mydomain .htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /mydomain/
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteRule ^post/([0-9]+)/?$ post.php?id=$1 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+)/?$ try.php?id=$1 [L,QSA]
Changes:
Added RewriteBase at the top.
Added L flag to mark it last rule.
Removed a redundant rule.
Moved specific rule above the generic rule.
Added required options.