Removing string from URL using .htaccess
I have URLs in the form of exam开发者_如何学编程ple.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
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.
精彩评论