I have a question about my HTACCESS. I've been experimenting with my HTACCESS for a while, and finally got the url rewriting to work - in combination with my own written php router.
I want to rewrite the url's not only to make them appear cleaner but also because i use it for the api. So i can do a GET/POST/PUT/DELETE to api/users and add a new user, update or remove etc.
Now i use these lines for the URL rewrite:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .*/api/(.*)$ api.php [QSA,L]
for the subdomain rerouting to a folder i use
RewriteCond %{HTTP_HOST} ^demo\.example\.com [NC]
RewriteRule (.*) http://example.com/demo/$1 [L,R=301]
The only problem now is that i can not seem to get these combined. I would like to have a url like:
demo.domain.com/api/user
If i use the two apart from each other they work like expected, but combined the url rewrite for the /api always fails. This is probably due to the rewrite condition of the subdomain, but i can't see how i can combine the two.
I found the solution, also i extended the htacces.. Here is my htaccess file:
# No Caching for page files
<filesMatch "\.(html|htm|js|css|php)$">
<IfModule mod_headers.c>
Header set Cache-Control "no-cache, no-store, must-revalidate"
Header set Pragma "no-cache"
Header set Expires 0
</IfModule>
</filesMatch>
# Error pages
ErrorDocument 404 https://%{HTTP_HOST}/#/404
ErrorDocument 500 https://%{HTTP_HOST}/#/500
RewriteEngine On
# Rewrite www to non
RewriteCond %{HTTP_HOST} !=""
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R,L]
# Rewrite http to https
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
# Ignore conditions for files and folders
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteBase /
# Rewrite /api to api.php
RewriteRule ^api/(.+)$ api/api.php [QSA,L]
# Rewrite /downloads to downloads.php
# RewriteRule ^download/(.+)$ download/download.php [QSA,L]
Related
Hello I want to redirect my http requests to https. I add some code to .htaccess but still I can access to my website with http. What's wrong and what should I do?
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule . /index.html [L]
</IfModule>
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteOptions inherit
Header set content-Security-Policy: upgrade-insecure-requests
With your shown attempts, could you please try following htaccess rues set. Since you are using inherit option for Rules, so in case you want to apply https in all URLs then better put your htpps rules in your BASE(very first level folder wise) htaccess file and we need not to put them here, if you want to only apply https to URLs which are covered here then use following style rules.
Please make sure to clear your browser cache before testing your URLs.
<IfModule mod_rewrite.c>
RewriteOptions inherit
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule . /index.html [L]
</IfModule>
Header set content-Security-Policy: upgrade-insecure-requests
The following code redirect any URL of your domain to HTTPS version and set HSTS:
#Redirect HTTP a HTTPS
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#HSTS
<if "%{HTTPS} == 'on'">
Header always set Strict-Transport-Security "max-age=63072000; includeSubDomains; preload"
</if>
I have analyzed my website with the portal web.dev from Google. This told me that I have too many redirects if I call the URL without www and with http:
It goes first to https and without www, then to http with www and only then to https and www.
This is my htacces file:
<IfModule mod_headers.c>
# Allow access from all domains for webfonts (see contao/core-bundle#528)
<FilesMatch "\.(ttf|ttc|otf|eot|woff2?|font\.css)$">
Header set Access-Control-Allow-Origin "*"
</FilesMatch>
</IfModule>
<files serviceaccount.json>
Order allow,deny
Deny from all
</files>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteCond %{HTTP:Authorization} .
RewriteRule ^ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^app\.php(?:/(.*)|$) %{ENV:BASE}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^ %{ENV:BASE}/app.php [L]
</IfModule>
I don't see at all where these redirects are implemented. I also found this variant to redirect directly. Even if I put them in the htaccess file, the redirects remain as described above.
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)$ [NC]
RewriteRule (.*) https://www.%1/$1 [R=301,L]
</IfModule>
Can you tell me how all these redirects come about and why the last code snippet doesn't work?
Some of those redirects might be done by your Hoster. Also, in newer Contao versions, Contao automatically redirects to https in the front end, if HTTPS is enabled in the respective website root's settings in the back end.
In any case, you should implement the redirects yourself, e.g. in your web/.htaccess. For example:
# Redirect to www subdomain
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Redirect to HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Note: the www redirect would redirect any domain that does not start with www. to www.. If you have a multidomain setup with different subdomains, you will need to adjust the www. redirect accordingly.
I have two separate .htaccess files that I'd like to be fused together so that the first rewrite always takes precedence, which redirects some traffic to https base which domain. Then if the file at the url does not exist, then it sends the traffic to a php file. It is a url shortener, but if that returns at 404, then it shows a 404 error page.
Here are the bits and pieces of the .htaccess files:
This, below, I believe should redirect all http traffic except kore.tt and korett.com to https:
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^(www\.)?(kore\.tt|korett\.com) [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
This is something from the url shortener that is supposed to send traffic that doesn't exist to loader.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /loader.php [L]
</IfModule>
But then if that returns a 404 error. Then this is the simple 404 error catch.
ErrorDocument 404 /404.html
You can use a condition in your htaccess. There are many more ways to create rules. See if this can work for you.
#check to see if loader.php exists on the filesystem, then do rewrite
<If "-f %{DOCUMENT_ROOT} . '/loader.php'">
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /loader.php [L]
</If>
#otherwise redirect to 404 page
<Else>
RewriteEngine On
RewriteRule ^ /404.html [R=404,L]
</Else>
I want to rewrite the urls like
site.com/eng?sentence=a-sentence
to be like:
site.com/eng/a-sentence
I only want the text shown in the brower url to change, I dont want a redirect.
RewriteCond %{THE_REQUEST} ^GET\s/+eng?sentence=([^/?&\s]+) [NC]
RewriteRule ^ /eng/%1 [NC,L,NE]
I have fiddled with it, comparing other rewrite rules i found for a long time, but just cant get it to work, And i really can't figure out why. I tried removing all other rewrite from my htaccess everytime i tested.
here is my htaccess file:
# Use PHP5.3 Single php.ini as default
AddHandler application/x-httpd-php54s .php
AddType application/x-httpd-php .htm .html
Options -Indexes -MultiViews
ErrorDocument 404 /404.php
ErrorDocument 500 /500.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteCond %{THE_REQUEST} ^GET\s/+eng?sentence=([^/?&\s]+) [NC]
RewriteRule ^ /eng/%1 [NC,L,NE]
<FilesMatch "\.(jpg|jpeg|png|gif|swf)$">
Header set Cache-Control "max-age=604800, public"
</FilesMatch>
preferably, if you could fix my rule rather than write your own one that I won't likely understand, that would be great.
I only want the text shown in the browser url to change, I dont want a
redirect.
To change URL in browser you need external redirect using R flag.
Also ? is special regex symbol and to match a literal ? you need to escape it like \?. Also you would need a ? in the target URI's end to strip off query string.
Your complete .htaccess:
RewriteEngine On
# actual oneto pretty URL - external redirect
RewriteCond %{THE_REQUEST} \s/+eng\.php\?sentence=([^&\s]+) [NC]
RewriteRule ^ /eng/%1? [L,NE,R=302]
# pretty URL to actual one - internal rewrite
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^eng/([^/]+)/?$ /eng.php?sentence=$1 [L,QSA,NC]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ /$1.php [L]
I highly recommend you to read: Apache mod_rewrite Introduction
i want to rewrite urls like
mysite.com/eng?sentence=cat
to show like:
mysite.com/eng/cat
I dont want a redirect just a rewrite. so I tried altering a rewrite rule i found, the example before i edited it had a R=301 in it originally:
RewriteCond %{THE_REQUEST} ^GET\ /eng\?sentence=([^&\s]+) [NC]
RewriteRule ^eng\?sentence=$ /english/%1? [R=302,NC,L]
but when i remove the R=301 from the 2nd line it gives a 500 error.
how can I write this kind of rewrite rule? Im not great with this regex stuff, nor am i familiar with this
my htaccess file:
# Use PHP5.3 Single php.ini as default
AddHandler application/x-httpd-php54s .php
AddType application/x-httpd-php .htm .html
Options -Indexes -MultiViews
ErrorDocument 404 /404.php
ErrorDocument 500 /500.php
RewriteEngine on
RewriteCond %{THE_REQUEST} ^GET\ /eng\?sentence=([^&\s]+) [NC]
RewriteRule ^eng\?sentence=$ /english/%1? [R=302,NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
<FilesMatch "\.(jpg|jpeg|png|gif|swf)$">
Header set Cache-Control "max-age=604800, public"
</FilesMatch>
Use line end anchor $ OR else just reply on RewriteCond
RewriteCond %{THE_REQUEST} ^GET\s/eng\?sentence=([^&\s]+) [NC]
RewriteRule ^ /eng/%1? [NC,L,R=302]
Most likely 500 is because of looping due to other rules in your .htaccess. I suggest you posting full .htaccess code in your question.