Apache mod_rewrite redirect, keep sub-directory name
I'm wondering if this is possible.
I have a single page site in which I'd like to incorporate a trailing slash with a file name that anchors to a section on that site. I'm trying to avoid using hash or hash-bangs.
For example; www.example.com/recent
Right now, I'm removing any trailing slash, but I get a 404 with /recent
because it's expecting a file.
RewriteRule ^(.*)/$ /$1 [R=301,L]
Is it possible to redirect to www.example.com
, but still maintain the /recent
without the server thinking it's a file so I can read it client-side (php/js)开发者_StackOverflow? More so that I can keep using the back and forward buttons.
Thanks for any help!
TBH it is not 100% clear for me what you want. As I understand you want URL www.example.com/recent
to be rewritten (internal redirect, when URL remains unchanged in browser) to www.example.com/index.php?page=recent
(or something like that).
DirectorySlash Off
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# remove trailing slash if present
RewriteRule ^(.*)/$ /$1 [R=301,L]
# do not do anything for already existing files
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .+ - [L]
# rewrite all non-existing resources to index.php
RewriteRule ^(.+)$ /index.php?page=$1 [L,QSA]
With the above rules (that need to be placed in .htaccess in website root folder) this can be achieved. Request for www.example.com/recent
will be rewritten to www.example.com/index.php?page=recent
so your single-page server side script knows which URL was requested. The same will be with any other non-existing resource e.g. www.example.com/hello/pink/kitten
=> www.example.com/index.php?page=hello/pink/kitten
.
It may not be necessary to pass originally requested URI as a page
parameter as you should be able to access it in PHP via $_SERVER['REQUEST_URI']
anyway.
If I misunderstood you and this is not what you want then you have to clarify your question (update it with more details, make it sound clear).
精彩评论