.htaccess dynamic to static URL

2019-09-02 01:46发布

I'm trying to make my dynamic URL's into static looking URL's. This is a typical URL that I now have: http://www.somedomain.com/design/index.php?p=about

I would like it to be: http://www.somedomain.com/about

So far, I've gotten it to look like this: http://www.somedomain.com/design/about.html

This is the Rewriterule I'm using: RewriteRule ^([a-z]+).html$ index.php?p=$1 [L]

How would I modify it so it would look like this: http://www.somedomain.com/about?

Thanks for any/all help!!! Very much appreciated!

标签: .htaccess
2条回答
Fickle 薄情
2楼-- · 2019-09-02 02:10

Rick, you're on the right track. You need to read the Apache rewrite documentation. For your docroot/.htaccess start it with:

 RewriteEngine On
 RewriteBase   /

Then generalised version of your rule:

 Rewrite Rule ^(\w+)$     index.php?p=$1 [L]

This will rewrite any requests which are for a word string to index.php. You need to be aware that the rewrite engine rescans the .htaccess file if a match has occured so you need to make sure that you don't create a loop. In this case the replacement string has a "." in it and the pattern doesn't, so this won't occur, but for more complex cases you may need to 'guard' the rules with one or more RewriteCond statements. Again, read the Apache documentation.

查看更多
乱世女痞
3楼-- · 2019-09-02 02:26

Using rewrite rules to give 'static' URI is NEVER a good idea.

A few other ideas you can use:

  • Make the 'about' page a directory (folder) with a file called index.php or index.html in it. This way the URL shows http://example.com/about/ and the information you wish can still be displayed as needed.
  • Use the POST method instead of GET methods. This will display as http://example.com/about.php (Note: there is no ? or other parameters behind that.)
  • Utilize both methods to give a 'seamless' URI on page transitions.
查看更多
登录 后发表回答