简体   繁体   English

使用.htaccess删除php扩展名

[英]remove php extension using .htaccess

I'm working on script, and want to set links to be as following: 我正在处理脚本,并希望将链接设置如下:

www.mysite.com/sign-up.php
to
www.mysite.com/sign-up

and

www.mysite.com/profile.php?username=abc
to
www.mysite.com/profile/abc

I found this code and works for me. 我找到了这段代码并为我工作。

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+)$ $1.php [L,QSA]

but when I want to access: 但是当我想访问时:

www.mysite.com/profile/abc

The stylesheet doesn't work, it seems the path for it has changed. 样式表不起作用,似乎它的路径已更改。 Also all links in that profile become like: 该个人资料中的所有链接也将变为:

www.mysite.com/profile/ profile /filename.php www.mysite.com/profile/ 个人资料 /filename.php

It keeps adding /profile everytime 每次都不断添加/profile

how to fix this? 如何解决这个问题?

EDIT: all files works fine with stylesheet except profile.php 编辑:除profile.php外,所有文件均可与样式表配合使用

You can create it with htacess and modrewrite of apache. 你可以创建htacessmodrewrite的Apache。

Just check the link below for generating dynamic URL. 只需检查以下链接即可生成动态URL。 It might solve your problem: 它可以解决您的问题:

URL Generation URL生成

Have you tried this? 你有尝试过吗?

RewriteRule ^(.*)$ $1.php

Just replace you current rule with this one 只需用此规则替换您当前的规则

www.mysite.com/sign-up.php to www.mysite.com/sign-up www.mysite.com/sign-up.phpwww.mysite.com/sign-up

   RewriteEngine on
   RewriteRule ^(.*)$ $1.php [r=301,L]

It is required to store this file to folder www.mysite.com/profile/.htaccess 需要将此文件存储到文件夹www.mysite.com/profile/.htaccess

   RewriteEngine on /profile/
   RewriteRule ^(.*)$ profile.php?username=$1 [r=301,L]

www.mysite.com/profile/abc to www.mysite.com/profile/profile.php?username=abc www.mysite.com/profile/abcwww.mysite.com/profile/profile.php?username=abc

The problem is that, first script will redirect string to string.php so www.mysite.com/profile/abc to www.mysite.com/profile/abc.php or www.mysite.com/profile/profile.php?username=abc.php . 问题是,第一个脚本将重定向stringstring.php所以www.mysite.com/profile/abcwww.mysite.com/profile/abc.phpwww.mysite.com/profile/profile.php?username=abc.php We must create an exception: 我们必须创建一个例外:

   RewriteEngine on
   RewriteRule ^profile/(.*)$ profile.php?username=$1 [r=301,L]
   RewriteRule ^(.*)$ $1.php [r=301,L]

We can create the whole script as an exception. 我们可以创建整个脚本作为例外。 This will simple do all the work. 这将简单地完成所有工作。

This isn't a problem with your rewrite rules, it's a problem with your href's. 这不是您的重写规则的问题,而是您的href的问题。

If you have a style tag like... <link rel="stylesheet" type="text/css" href="css/main.css" media="screen" /> and an a tag like <a href="profile"> , they will be treated as relative URLs since there is no forward slash at the start. 如果您有一个样式标签,例如... <link rel="stylesheet" type="text/css" href="css/main.css" media="screen" />和一个标签,例如<a href="profile"> ,它们将被视为相对URL,因为开始时没有正斜杠。 If your browser is at /profile.php, it will load /css/main.css and the link goes to /profile, but once you are at /profile, the browser thinks you are in that "folder", so it will now look at /profile/css/main.css and your link will go to /profile/profile. 如果您的浏览器位于/profile.php,它将加载/css/main.css并且链接指向/ profile,但是一旦您位于/ profile,浏览器就会认为您位于该“文件夹”中,因此现在查看/profile/css/main.css,您的链接将转到/ profile / profile。 You need to make the href's absolute paths, ie href="/css/main.css" and href="/profile". 您需要制作href的绝对路径,即href =“ / css / main.css”和href =“ / profile”。

If your CSS link is broken amongst other things, try using the base tag in your document head: 如果您的CSS链接断开了,请尝试使用文档头中的base标签:

<head>
<base href="http://www.mysite.com/" />
</head>

I have found this to work when dealing with pesky URL rewrites. 我发现这在处理讨厌的URL重写时有效。

There may be better ways to solve it via .htaccess, but this might be a nice short term fix until that happens. 可能有更好的方法可以通过.htaccess解决此问题,但这可能是一个不错的短期解决方案,直到这种情况发生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM