在.htaccess中重写php url

在.htaccess中重写php url,php,apache,url,rewrite,Php,Apache,Url,Rewrite,我已经尝试了所有我能做的,但没有结果,检查了所有关于url.htaccess重写的web教程,没有一个专门解决了我的所有问题,我的编程专家帮我解决了这个问题,我有一个php web应用程序,.htaccess代码我只管理url,没有.php扩展名,比如localhost/app/images.php,链接是localhost/app/images,一旦你点击它,.htaccess就会理解/images是/images.php并获取文档,但是当我试图把更多的.htaccess代码放在正确的链接位置

我已经尝试了所有我能做的,但没有结果,检查了所有关于url
.htaccess
重写的web教程,没有一个专门解决了我的所有问题,我的编程专家帮我解决了这个问题,我有一个php web应用程序,.htaccess代码我只管理url,没有.php扩展名,比如
localhost/app/images.php
,链接是
localhost/app/images
,一旦你点击它,.htaccess就会理解
/images
/images.php
并获取文档,但是当我试图把更多的.htaccess代码放在正确的链接位置时,它会重写一些动态链接,如
/images/muscellaneous

/images.php?album_id=miscellaneous
我明白了

internal server error
这是我现在拥有的.htaccess代码,它只匹配/images到/images.php

# Turn on URL rewriting
RewriteEngine on
RewriteBase /ansjc
# Remove file extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) $1.php [L]
RewriteRule images/album_id/(.*)/ images.php?album_id=$1
RewriteRule images/album_id/(.*) images.php?album_id=$1 

您的重写规则有两个主要问题:

  • 他们的顺序很重要。现在,你的第二个和第三个永远不会匹配
  • 其中两个可以简化为一个
考虑使用以下方法:

 RewriteEngine on
 RewriteBase /ansjc
 # Remove file extension
 RewriteRule images/album_id/(.+)/?$ images.php?album_id=$1 [L]
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteRule (.+) $1.php [L]
[L]标志表示“最后”。换言之,如果它匹配,就不会处理重写。因此,如果images/album_id/etc/匹配,第二条重写规则将不会产生干扰


这也解决了将.php附加到所有内容的问题。尽管我怀疑您的500可能来自您的代码,而不是重写。

您的重写规则有两个主要问题:

  • 他们的顺序很重要。现在,你的第二个和第三个永远不会匹配
  • 其中两个可以简化为一个
考虑使用以下方法:

 RewriteEngine on
 RewriteBase /ansjc
 # Remove file extension
 RewriteRule images/album_id/(.+)/?$ images.php?album_id=$1 [L]
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteRule (.+) $1.php [L]
[L]标志表示“最后”。换言之,如果它匹配,就不会处理重写。因此,如果images/album_id/etc/匹配,第二条重写规则将不会产生干扰

这也解决了将.php附加到所有内容的问题。尽管我怀疑您的500可能来自您的代码,而不是重写。

使用此代码

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^([^/]*)$ $1.php [NC,L]
RewriteRule ^images/(.*)$ images.php?album_id=$1 [L]
试一试

http://localhost/images
http://localhost/images/
http://localhost/images/album_id
它将调用images.php和inside images.php just print\r($\u GET);要进行测试。

请使用此代码

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^([^/]*)$ $1.php [NC,L]
RewriteRule ^images/(.*)$ images.php?album_id=$1 [L]
试一试

http://localhost/images
http://localhost/images/
http://localhost/images/album_id

它将调用images.php和inside images.php just print\r($\u GET);测试。

此解决方案有效,但删除了样式表,并且没有显示任何数据,我想是我出错了,我的链接就是这样开始的:echo“
  • 因此,单击链接后,我希望.htaccess将变量传递给images.php?album\u id页面=Miscellaneous@NwachukwuAlNnaemeka:请记住,images.php突然无法神奇地理解它现在比原来的位置低了两个目录。如果您的样式表和图像是相对指定的(href=“css/styles.css”),则需要将它们调整为绝对值。此解决方案可以工作,但会删除样式表,并且不会显示任何数据,我认为是我犯了错误,我的链接就是这样开始的:echo“
  • 因此,单击链接后,我希望.htaccess将变量传递给images.php?album\u id页面=Miscellaneous@NwachukwuAlNnaemeka:请记住,images.php突然无法神奇地理解它现在比原来的位置低了两个目录。如果样式表和图像是相对指定的(href=“css/styles.css”),则需要将它们调整为绝对值。