Abzax
Abzax

Reputation: 71

How to remove index.php from URLs

Am wondering how to remove index.php from URLs, every link on my site has index.php in it, for e.g. example.com/index.php/contact-us.

Am using Drupal 8, my server is Apache and php version is 5.6, and am on Shared Hosting.

In the .htaccess file i tried to use

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]

That removed the index.php but any new article wont show the images in the homepage(index.php)

then i tried to use

RewriteBase /
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]

Same thing happened.

Please advice

Upvotes: 2

Views: 4835

Answers (2)

tbart
tbart

Reputation: 91

This is what I added to my apache config.

# Sometimes (multiple instances of) index.php/ get(s) inserted in URLs
# Remove them
RewriteCond %{REQUEST_URI} ^/(index.php/)+(.*)
RewriteRule ^.*$ /%2 [R=301,END]

Apparently, some "simple XML sitemap" version introduced these index.php parts and submitted them to search engines and now I get hits with those unclean urls.

Configuring drupal to not use them is the one thing, but telling clients those URLs are not valid anymore is another thing. That's where the above comes in handy. It simply removes all index.php/ parts in a URL.

NB: This has to be placed BEFORE all other rewrite rules!

Upvotes: 3

VJamie
VJamie

Reputation: 636

Please take a look at the following Drupal 8 documentation: https://www.drupal.org/docs/8/configuring-clean-urls/enable-clean-urls

This should help you to enable clean URLs.

Additionally: You might want to check the following issue and comments for your problem: Enable Clean URLs

Extension: Try checking the src URL on your images (If they contain the index.php, if not it might be a permission misconfiguration)

Edit: Try this code in your .htaccess

  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_URI} !=/favicon.ico
  RewriteRule ^ index.php [L]

On a side note: You might want to consider switching to PHP7 since active support for PHP5.6 will end soon. (Source)

Upvotes: 0

Related Questions