Skip to content
Advertisement

htaccess url rewrites and redirects not working

I have a website that was updated. I want to make the old urls work, I want to redirect some of them, so links from google search still work, and rewrite the url for other urls, using .htaccess.

For example:

  • one rewrite is a.com/services to a.com/consultancy
  • one redirect is a.com/services/a/b to a.com/consultancy

One extra comlpication: the site sends every request to /index.php because the URLs are not to physical files, but the index.php script does internal routing, using the requested path, to serve the right content.

I’m not sure if I can do a rewrite (services->consultancy) an then another rewrite (consultancy->index.php) in the same htaccess.

Trying this, I’m getting an internal server error 500 for any URL:

RewriteEngine On

#NEW
RewriteCond %{REQUEST_URI} ^services$ [NC]
RewriteRule ^services$ consultancy [L]

#LEGACY
RewriteRule (.*) ./index.php

Also tried the Redirect 301 directive but had no luck, the same error 500.

Any ideas of how to mix rewrites, redirects and the final rewrite to index.php?

Thanks!

Update

For the combination of redirect and rewrite I realized some of the rules are a little different than my original question, and those might be causing problems, here is an example:

# Redirect to show "consultancy" in the URL
RewriteRule ^services/a/b?$ consultancy [QSA,R,L,NC]

# If "consultancy" is in the URL, change it to the real path
# "consultancy" is an alias of the path that should be processed by index.php
RewriteRule ^consultancy/?$ en/consultoria [NC]

# Should get the "en/consultoria" path
RewriteRule ^ ./index.php [L]

The problem with the previous example is that I get the redirect “services/a/b” -> “consultancy” OK, but the rewrite “consultancy” -> “en/consultoria” is not done.

Any ideas?

Advertisement

Answer

You can use:

RewriteEngine On

#rewrite
RewriteRule ^services/?$ consultancy [L,NC]

#redirect
RewriteRule ^services/a/b/?$ consultancy [L,NC,R=302]

#LEGACY
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement