Date and name based friendly URL using .htaccess

大城市里の小女人 提交于 2021-02-05 09:04:49

问题


I'm planning on switching from having my blog on wordpress.com to having it on my own site hosted on my own server. Naturally I want to preserve the link structure from wordpress so that no links to my blog out there on the WWW break.

So my question is, how do I get the following friendly URL

http://example.com/yyyy/mm/dd/post-name

to map to some structure like

http://example.com/index.php?page=bla-bla

on my server using .htaccess?

Can the identifier bla-bla be a simple integer id, or does it have to include both date and name, that would be yyyy-mm-dd-post-name?

Thanks in advance.


EDIT: Thank you for your quick replies. A few additions to my question though: I'm not currently using wordpress.org, so I don't have a control panel that can generate the file for me. At least I think not. And I'm also not going to use wordpress on my own server.


回答1:


You can make following work as you require .. put it in a .htaccess file.

# Options +FollowSymLinks  
# RewriteEngine On  
#   
# RewriteCond %{SCRIPT_FILENAME} !-d  
# RewriteCond %{SCRIPT_FILENAME} !-f  
#   
# RewriteRule ^page/(\d+)*$ ./page.php?id=$1 

To read all of the details .. click here to read "friendly and pretty urls"




回答2:


There's a setting on the WordPress control panel that you can activate, and a .htaccess file provided with WordPress that lets it work. It lets you set up the URL's any way you want.




回答3:


WordPress has permalinks built-in and will create an appropriate .htaccess file for you. If you're going to use WordPress on your own domain as well, that is.




回答4:


If your host is a Linux server, the rewrite will be supported out of the box. If you host is a Windows server, you will need a rewrite plugin installed.

Windows server is running Helicon Tech ISAPI_Rewrite v3, and it works perfectly fine after I:

Turned the plugin on

In Helm: Home > Domains >  domain_name > Website Extensions > URL Rewriting

Wrote my rewrite correctly

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /blog/index.php [NC,L]
<blank line>

Changed my WordPress permalinks to custom

/%year%/%monthnum%/%day%/%postname%/


来源:https://stackoverflow.com/questions/1235903/date-and-name-based-friendly-url-using-htaccess

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!