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 Ios抖音直播的时候如何添加自定义图片在直播间!
  • ¥60 riscv-pulpino总线上挂载axi从机
  • ¥15 ssh登录页面的问题
  • ¥60 渗透一个指定银行app,拿到客户信息,需要什么级别
  • ¥50 关于在matlab上对曲柄摇杆机构上一点的运动学仿真
  • ¥15 jetson nano
  • ¥15 :app:debugCompileClasspath'.
  • ¥15 windows c++内嵌qt出现数据转换问题。
  • ¥15 stm32 串口通讯过程中的问题
  • ¥20 公众号如何实现点击超链接后自动发送文字