Change URL display but stay on the site

I know this has already been asked but I never found an answer that worked for me. I tried understanding the mod_rewrite myself but I always got stuck. I searched a lot online and I’ve read the Apache documentation, Stackoverflow posts and a few tutorials (addedbytes/ smashingmagazine/ selfhtml/ and so on…).

Every URL the user sees looks like this at the moment: www.example.com/text/show/1 The number at the end varies, because the texts are numbered by the CMS. I would like to show the user an URL like the following: www.example.com/home while he visits www.example.com/text/show/1. Of course this should be possible on other sites as well. (Hint: The pages aren’t numbered continously.)

I want to rewrite the URL the user visits, using the .htaccess. The user should stay on the same page but he should see a nicer URL. I don’t have any file extensions, because I’m using a CMS from a coworker that works without them.

This is my .htaccess:

# Enable rewriting
RewriteEngine On

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

# Redirect old PHP-pages to new pages
Redirect /fancy.php http://www.example.com/text/show/6
Redirect /foobar.php http://www.example.com/text/show/12
# Here are a few more redirects

# URL rewriting
RewriteRule ^home/?$ text/show/1 [NC,L]

# Skip the bli and bla directories
RewriteRule ^(bli|bla)($|/) - [L]

# Skip existing files and directories
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Rewrite the URL
RewriteRule ^(.*)$ index.php [QSA,L]

I need the redirects because some pages of our old version of the websites where we used no CMS are still available online. The last parts (After Skip the bli...) are given from the CMS so I can’t change them. I enabled the mod_rewrite on the apache so this should be the problem. I also reloaded and/ or restarted the apache after my changes.

Everything else works well but the RewriteRule ^home/?$ text/show/1 [NC,L] doesn’t. Can somebody please tell me how I can fix this?

PS: I’m testing this on a local domain so I don’t make any mistakes online.


Source: apache

Leave a Reply