douchuo1963 2013-01-21 18:37
浏览 76
已采纳

删除URL中的.php扩展名

I previously changed my .htaccess file so that my 'subdirectory' (13/) can be the 'directory' I use for my main domain by adding :

RewriteCond %{HTTP_HOST} ^(www.)?mydomain.com$
RewriteRule ^(/)?$ 13/index.php [L]

Now I want to remove the .php extensions and extensions like index.php?eventid=3 to index/something

EDIT#1[where something is the name of that id stored in the database]

How should I do that? Previously, I did work in some PHP-frameworks but they already have a dedicated url file. So I never thought much about it. But this time, I'm not using any framework, so I need to modify .htaccess.

I looked around, but was unable to find any reliable blog/sources.

EDIT #2

My sub-directory is public_html/13/.

ie, www.mydomain.com redirects to www.mydomain.com/13/ Now I want to access www.mydomain.com/13/events.php?eventid=1 as www.mydomain.com/events/44

  • 写回答

2条回答 默认 最新

  • duanhuangyun3887 2013-01-21 18:49
    关注
    RewriteRule ^([A-Za-z0-9\-]+)/([A-Za-z0-9\-]+)/([0-9]+)/?$ $1.php?$2=$3
    

    The above code would redirect http://www.mydomain.com/index/eventid/3/ to http://www.mydomain.com/index.php?eventid=3

    htaccess is very flexible, and creating variations of the above is pretty easy once you understand the syntax. You will want to read up on the regular expressions you can use, and the myriad of options the Rewrite module has to offer. Most of my redirects look like the above though, real simple.

    EDIT: An example of what you're looking for is Wordpress, which uses slugs to identify pages based on something more semantic than an ID number. You would have to url encode the name of the item from the database and either store it in the database as the slug, or find some other way of making it usuable in the database query that retrieves the content of the page. Then your htaccess would look something like this:

    RewriteRule ^([A-Za-z0-9\-]+)/([A-Za-z0-9\-]+)/?$ $1.php?slug=$2
    

    In your PHP you will need to create slugs and store them in the db so they can be used as IDs in your queries.

    EDIT2: Try changing your entire htaccess file to just this:

    <IfModule mod_rewrite.c>
    RewriteEngine on
    
    RewriteCond %{HTTP_HOST} ^www\.yourdomain\.com$ [NC]
    RewriteRule ^(.*)$ http://yourdomain.com/$1 [R=301,L]
    
    RewriteRule ^([A-Za-z0-9\-]+)/([A-Za-z0-9\-]+)/([0-9]+)/?$ $1.php?$2=$3
    
    </IfModule>
    

    I have tested it and it works. Something else in your htaccess is messing it up.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 前端echarts坐标轴问题
  • ¥15 CMFCPropertyPage
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳