1

i made a website by expressionengine, in the htaccess i need to remove the index.php and also add www to none www url's. here is my current .htaccess:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    # Add www to url
    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

    # Directs all EE web requests through the site index file
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L,QSA]

    # Removes index.php from ExpressionEngine URLs
    RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
    RewriteCond %{REQUEST_URI} !/system/.* [NC]
    RewriteRule (.*?)/index\.php/*(.*) /$1$2 [R=301,NE,L]

</IfModule>

but the above code has some problems, it works when the url has the www itself and there is no index.php anywhere and everything is fine, but when i remove the www from the url to test the "add www" part it does not work any more and the url damages lik this:

http://www.example.com/index.php?/segment1/segment2/

an index.php comes up. i don know what to do, any help will be appriciated

2 Answers 2

1

Try this..

RewriteEngine On

#Redirect non-www to www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

it will remove index.php from the URL and add www as a prefix.

Sign up to request clarification or add additional context in comments.

3 Comments

actually it works individually, the problem is when i want to remove index.php and add www
@Ajay - after trying many rule, finally this code worked for me.
Always here to help :)
0

The www redirect probably doesn't work because there's no wildcard, keep it simple and specify your domain:

# Redirect to www
# -------------------
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

I expect you have the removal and addition of index.php the wrong way around, I redirect it first if specified in the URL, then add it in hidden:

# Redirect specific index.php requests in URL
# ------------------------------
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,L]

# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]

Also try with and without the ? on this line, depending on the server setup, this might not be needed:

RewriteRule ^(.*)$ index.php?/$1 [L,QSA]

or

RewriteRule ^(.*)$ index.php/$1 [L,QSA]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.