Asked  6 Months ago    Answers:  5   Viewed   125 times

I have a problem whereby google has indexed some pages with the wrong url.

The url they are indexing is:

I need it to redirect to:

.htaccess isn't my forte, so any help would be much appreciated.

Thanks in advance.



The original answer is actually correct, but lacks explanation. I would like to add some explanations and modifications.

I suggest reading this short introduction (15mins) and reference these 2 pages while reading.

This is the basic rule to hide index.php from the URL. Put this in your root .htaccess file.

mod_rewrite must be enabled with PHP and this will work for the PHP version higher than 5.2.6.

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

Think %{REQUEST_FILENAME} as the the path after host.

E.g., %{REQUEST_FILENAME} is /index.html

So the last 3 lines means, if it's not a regular file !-f and not a directory !-d, then do the RewriteRule.

As for RewriteRule formats:

enter image description here

So RewriteRule (.*) /index.php/$1 [L] means, if the 2 RewriteCond are satisfied, it (.*) would match everything after the hostname. . matches any single character , .* matches any characters and (.*) makes this a variables can be references with $1, then replace with /index.php/$1. The final effect is to add a preceding index.php to the whole URL path.

E.g. for, it would produce, internally.

Another key problem is that this indeed solve the question. Internally, (I guess) it always need, but with rewriting, you could visit the site without index.php, apache adds that for you internally.

Btw, making an extra .htaccess file is not very recommended by the Apache doc.

Rewriting is typically configured in the main server configuration setting (outside any <Directory> section) or inside <VirtualHost> containers. This is the easiest way to do rewriting and is recommended

Tuesday, June 1, 2021
answered 6 Months ago

You can set general rule in the last line:

RewriteEngine on

RewriteRule    ^category/([A-Za-z0-9-]+)/?$    category.php?cat=$1    [NC,L]    
RewriteRule    ^author/([A-Za-z0-9-]+)/?$    author.php?auth=$1    [NC,L]  
RewriteRule    ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$    index.php?id=$1    [NC,L]   

This issue happened because first line support all urls

Wednesday, March 31, 2021
answered 9 Months ago

I had the same problem (trouble stripping 'www' from URLs that point to a sub-directory on an add-on domain), but after some trial and error, this seems to work for me:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [R=301,QSA,NC,L]
Wednesday, June 16, 2021
answered 6 Months ago

Your # Add trailing slash rule appears to be problem. Can you try commenting it out for testing.


Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /app/

## Adding a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} s/+(.+?[^/])[?s] [NC]
RewriteRule ^ /%1/ [L,R=301]

# remove /public/ from URL
RewriteCond %{REQUEST_URI} !/public/ [NC]
RewriteRule ^(.*?)/?$ public/$1 [L]
Friday, August 6, 2021
answered 4 Months ago

From your information I can only guess ("doesn't work" isn't very specific).
Most probably you did not enable mod_rewrite. Type in:

a2enmod rewrite

and restart Apache after:

service apache2 restart

Sunday, October 3, 2021
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :