简体   繁体   English

htaccess mod_rewrite规则组合问题

[英]htaccess mod_rewrite rules combination issue

I'm so newbie in apache, so it's a basic question but I couldn't solve this using another questions or links. 我是apache的新手,所以这是一个基本问题,但是我无法使用其他问题或链接来解决。
I tried to change my URL using .htaccess and I had two purposes; 我试图使用.htaccess更改URL ,但我有两个目的:

  1. hide .php extension 隐藏.php扩展名
  2. change some queryString from somefile.php?id=bar to somefile/id/bar which bar is a number or mixed string like T51-3 . 将一些queryString从somefile.php?id=bar更改为somefile/id/bar ,其中bar是数字或混合字符串,例如T51-3
    My queryString is http://localhost/payment/theme/ticket?id=770314 that I want change it to http://localhost/payment/theme/ticket/id/770314 or 我的queryString是http://localhost/payment/theme/ticket?id=770314 ,我想将其更改为http://localhost/payment/theme/ticket/id/770314
    http://localhost/payment/theme/ticket/770314

I found this code: 我发现此代码:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]


## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f [NC]
RewriteRule ^ %{REQUEST_URI}.php [L]


in stackoverflow.com/this_question stackoverflow.com/this_question中

This was working nice but didn't solve my second issue. 这个工作很好,但是没有解决我的第二个问题。
So, I searched for a while in this site and others and did find some sample codes like this one : 因此,我在该网站和其他网站中搜索了一段时间,并确实找到了一些像这样的示例代码:

RewriteRule ^([a-zA-Z0-9_-]+)-([0-9]+)$ ticket.php?id=$2

or 要么

RewriteRule ^(.*)$ /ticket.php?url=$1 [L]

and etc... 等等...

but none of these didn't work for me. 但是这些都不适合我。
Some of them will make the server done and some don't have any affect at all. 其中一些可以使服务器完成工作,而另一些则完全没有影响。 Would you please guide me to right way... 您能指导我正确的方法吗?
I looked at these questions: 我看着这些问题:
htaccess rewrite for query string htaccess重写查询字符串

how to change query string parameters with name using .htaccess? 如何使用.htaccess更改名称的查询字符串参数?

How can I use .htaccess to hide .php URL extensions? 如何使用.htaccess隐藏.php URL扩展?

And these links: 这些链接:
http://simonecarletti.com/blog/2009/01/apache-query-string-redirects/ http://simonecarletti.com/blog/2009/01/apache-query-string-redirects/
https://wiki.apache.org/httpd/RewriteQueryString https://wiki.apache.org/httpd/RewriteQueryString

UPDATE : 更新:
This is my site strucure: 这是我的网站结构: 网站结构
There is an htaccess for important folders, like, forms , db , classes etc... htaccess用于重要的文件夹,例如formsdbclasses等。
All my site pages are in the theme folder, as you can see in the picture. 如图所示,我所有的网站页面都位于theme文件夹中。
In the root htaccess, there's just two lines that auto start sessions. 在htaccess根目录中,只有两行自动启动会话。

And I added your code to theme's htaccess. 我将您的代码添加到主题的htaccess中。
Thanks in Advance

You can use this code in 您可以在

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /payment/theme/

# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php\?(\w+)=([^\s&]+)\s [NC]
RewriteRule ^ %1/%2/%3? [R,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]

# to handle http://localhost/payment/theme/ticket/id/770314
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(\w+)/([\w-]+)/([\w-]+)/?$ $1.php?$2=$3 [L,QSA]

## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]

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

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