Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

shahr's avatar
Level 10

.htaccess

I want to go to public/index.php with this code. I want to read .txt and .html, and I want to https

RewriteEngine on

# Redirect HTTP to HTTPS
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Rewrite requests to /public to the index.php file inside the /public directory
# Remove public folder form URL
RewriteRule ^(.*)$ public/ [L]

# Serve the Google file verification or other files with their correct headers
<FilesMatch "^(google[a-f0-9]{16}\.html|.*\.txt)$">
    Header set Content-Type "text/plain"
</FilesMatch>
0 likes
4 replies
LaryAI's avatar
Level 58

The provided code in the question is already doing what the user wants. It redirects HTTP to HTTPS, removes the public folder from the URL, and serves the Google file verification or other files with their correct headers.

anilkumarthakur60's avatar

It looks like you have written some Apache mod_rewrite rules in your .htaccess file to redirect HTTP requests to HTTPS and rewrite requests to /public to the index.php file inside the /public directory. Additionally, you have specified some rules to serve the Google file verification or other files with their correct headers.

To read .txt and .html files, you can modify the following section of your .htaccess file:

<FilesMatch "^(google[a-f0-9]{16}\.html|.*\.txt)$">
    Header set Content-Type "text/plain"
</FilesMatch>

To serve .txt and .html files with their correct content types, you can update the "Content-Type" header as follows:

<FilesMatch "^(google[a-f0-9]{16}\.html|.*\.txt)$">
    Header set Content-Type "text/html"
</FilesMatch>

This will ensure that .txt and .html files are served with the correct content type, allowing the browser to display them properly.

If you want to ensure that all requests to public/index.php are redirected to HTTPS, you can add the following rule after the HTTPS redirect rule:

RewriteRule ^public/index\.php(.*)$ https://%{HTTP_HOST}/public/index.php [L,R=301]
		```
This will redirect all requests to public/index.php to the HTTPS version of the same URL.

I hope this helps!
shahr's avatar
Level 10

@anilkumarthakur60

Do you mean?

RewriteEngine on

# Redirect HTTP to HTTPS
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteRule ^public/index\.php(.*)$ https://%{HTTP_HOST}/public/index.php [L,R=301]

# Rewrite requests to /public to the index.php file inside the /public directory
# Remove public folder form URL
RewriteRule ^(.*)$ public/ [L]

# Serve the Google file verification or other files with their correct headers
<FilesMatch "^(google[a-f0-9]{16}\.html|.*\.txt)$">
    Header set Content-Type "text/plain"
</FilesMatch>

Snapey's avatar

he means he got that code from chat-gpt and you will need to try and understand it.

Please or to participate in this conversation.