开发者

mod_write RewriteRule

im sure this is simple but i totally cant get it to work :\ I'm trying to rewrite

http://www.example.com/products/ABC123

to

http://www.example.com/index.php?page=shop&code=ABC123

I have tried

RewriteBase /

RewriteRule ^products/([A-Za-z0-9]+)$ index.php?page=shop&code=$1

Which redirects OK but all the images (and js css etc) have the wrong path (example.com/images/ABC123.jpg is example.com/products/images/ABC123.jpg). Also all of the links are now the wrong path (example.com/?page=shop&folder=7 is example.com/products/?page=shop&folder=7)

My .htaccess file is in the root, I have also tried RewriteBase products/ and removing products/ from the rules regex but that just throws a 404

I've read the official docs concerning mod_rewrite several times in the last hour, I must be missing something?

** EDIT: ** Sorry all was a bit of a trick question, turns out you can solve this with the html BASE eleme开发者_如何学编程nt, there was nothing wrong with my regex at the start!

I put <base href="http://www.example.com/" /> in the HEAD section and it's all fine and dandy now!

Thanks to the guys who showed me how to set conditions for weather or not the file/directory exists


Try this rule in your .htaccess file:

Options +FollowSymlinks -MultiViews
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^products/([-a-z0-9]+)$ /index.php?page=shop&code=$1 [L,NC,QSA]


      RewriteEngine On
      RewriteBase /

      # Generic requests for the application
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule ^products/(.*)$ index.php?page=shop&code=$1 [L]

That do it?

EDIT: Ah, well, your in-page links being wrong is something you should probably fix in PHP. You might try this:

    RewriteEngine On
    RewriteBase /

    # Generic requests for the application
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d

    # Main page
    RewriteRule ^products/([A-Za-z0-9]+)$ index.php?page=shop&code=$1 [L]
    RewriteRule ^products/?(.*)$ index.php?$1 [L]

    # Resources
    RewriteRule ^products/images/(.*)$ images/$1 [L]
    RewriteRule ^products/css/(.*)$ css/$1 [L]
    RewriteRule ^products/js/(.*)$ js/$1 [L]

But really, you should generate your URLs in php so they work right: would be more efficient than having to run them through mod_rewrite every time.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜