Question

Redirecting domain.com/index.php to domain.com

Hi, I’ve been plugging away at this for almost 8 hours and can’t find a solution.

What I need to do is very standard. I just need /index.php to redirect or rewrite as domain.com

.htaccess files aren’t working even though I’ve allowed overrides in my VH.

I tried a different approach and just put the rewrite info right into VH which also failed as domain.com/index.php still resolves:

<VirtualHost *:80>
	ServerAdmin webmaster@localhost
	DocumentRoot /var/www/<ommitted>.co/public_html
	ServerName <ommitted>.co
	ServerAlias www.<ommitted>.co


	ErrorLog ${APACHE_LOG_DIR}/error.log
	CustomLog ${APACHE_LOG_DIR}/access.log combined

	RewriteEngine on
	RewriteCond %{SERVER_NAME} =www.<ommitted>.co [OR]
	RewriteCond %{SERVER_NAME} =<ommitted>.co
	RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
	RewriteCond %{THE_REQUEST} ^.*/index\.php
	RewriteRule ^(.*)index.php$ /$1 [R=301,L]
</VirtualHost *:80>

I’ve read so many tutorials, I have to believe it’s something else causing the issue. A second set of eyes would be AWESOME.

Thanks.


Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

Try removing the following line:

RewriteCond %{THE_REQUEST} ^.*/index\.php

Since the line below it matches URIs that end with /index.php only, you don’t need to add a condition for it. Also, don’t forget to escape the .php dot in ^(.*)index.php$.

    RewriteCond %{SERVER_NAME} =www.<ommitted>.co [OR]
    RewriteCond %{SERVER_NAME} =<ommitted>.co
    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]

What are these lines for? Are you redirecting all traffic from HTTP to HTTPS? The VirtualHost you posted is configured to listen on port 80 only—make sure you’re adding the rewrite rules to the right VirtualHost.