0

I have URLs in the form of example.com/pages/page1 and /example.com/pages/page2

Is there an easy way in .htaccess to get rid of the /pages/ section, so my URLs are:

example.com/page1

Thanks

3
  • Wait - please clarify so I understand your goal: Do you want your end-users typing in example.com/pages/page1 which redirects internally to example.com/page1 or do you want your end-users typing in example.com/page1 which redirects internally, silently to example.com/pages/page1? Commented May 12, 2011 at 13:13
  • I want users who type example.com/pages/page1 to be redirected to example.com/page1 etc Commented May 13, 2011 at 12:46
  • See edits to my answer. Hope this does it for you. Commented May 13, 2011 at 12:54

1 Answer 1

2

If you have the ability to modify the DocumentRoot, it sounds like you would just need to set your DocumentRoot to /path/to/pages. However, if you can't do that then you can try this in .htaccess

RewriteEngine On
RewriteRule ^/(.*)$ /pages/$1 [L,QSA]

The above is generic and redirects everything to /pages. If your pages really are called "page1 page2 etc", then this is more specific:

RewriteRule ^/page([0-9]+)$ /pages/page$1 [L,QSA]

Remove the [L] if you have more rewrite rules to process. The [QSA] forwards along any other querystring parameters that may have been present.

EDIT: For users to enter example.com/pages/page1 to be redirected to example.com/page1:

RewriteEngine On
RewriteRule ^/pages/(.*)$ /$1 [L,QSA]

The above will redirect internally but not change the browser's address bar. If you want the address bar to change, informing the user that the redirect has happened, use [L,R=301,QSA] instead.

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

1 Comment

My page names are dynamic, so are similar to /pages/about-us and /pages/contact. I have other pages, such as /categories and /users so I'd rather not redirect everything, just those that match /pages/dynamic_url

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.