开发者

htaccess, MySQL DB and non-existent files

开发者 https://www.devze.com 2023-03-26 02:11 出处:网络
I have content in a MySQL DB that I\'d like to display on my home page, depending on the URL the user came from. I don\'t want mod_rewrite to change my URL, but rather, to do the following:

I have content in a MySQL DB that I'd like to display on my home page, depending on the URL the user came from. I don't want mod_rewrite to change my URL, but rather, to do the following:

  • Keep URL (ie mysite.com/demolink2)
  • Check is the page exists, and if not, redirect to index.php, passing the "demolink2" variable (essentially the end of the URL string after last trailing slash)
  • On the index.php side I'll be able to then serve up the content, but again, I don't want the user to see the URL changed to mysite.com/index.php?id=demolink2, but rather, have the mysite.com/i开发者_高级运维ndex.php page STAY as mysite.com/demolink2 until they click a URL within the site that leads to a real page.

Here's my current .htaccess which accomplishes the first bit; i.e. it will see if the page already exists, and if not, direct user to index, however I've done that with a rewrite.

Any help/comments would be appreciated.

htaccess file:

Options +FollowSymLinks
RewriteEngine on

RewriteBase /

RewriteRule ^$ /index.php [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /index.php [L]


Just change the last line to

RewriteRule (.*) /index.php?id=$1 [QSA,L]

The URL in browser will remain the same as it is rewrite (internal redirect) and not proper 3xx code redirect.

  • If mysite.com/demolink2 is requested, your script will see it as /index.php?id=demolink2;
  • If it will be mysite.com/hello/kitten, then script will see index.php?id=hello/kitten;
  • QSA flag is added to preserve existing query string.
0

精彩评论

暂无评论...
验证码 换一张
取 消