简体   繁体   English

.htaccess mod_rewrite URL 查询

[英].htaccess mod_rewrite URL query

I was hoping someone could help me out.我希望有人能帮助我。 I'm building a CRM application and need help modifying the .htaccess file to clean up the URLs.我正在构建 CRM 应用程序,需要帮助修改 .htaccess 文件以清理 URL。 I've read every post regarding .htaccess and mod_rewrite and I've even tried using http://www.generateit.net/mod-rewrite/ to obtain the results with no success.我已经阅读了所有关于 .htaccess 和 mod_rewrite 的帖子,我什至尝试使用http://www.generateit.net/mod-rewrite/来获得结果但没有成功。 Here is what I am attempting to do.这是我正在尝试做的。

  • Let's call the base URL www.domain.com我们称基地 URL www.domain.com
  • We are using php with a mysql back-end and some jQuery and javascript我们正在使用 php 和 mysql 后端以及一些 jQuery 和 javascript
  • In that "root" folder is my .htaccess file.在那个“根”文件夹中是我的 .htaccess 文件。 I'm not sure if I need a .htaccess file in each subdirectory or if one in the root is enough.我不确定我是否需要在每个子目录中都有一个 .htaccess 文件,或者根目录中的一个文件是否足够。
  • We have several actual directories of files including "crm", "sales", "finance", etc.我们有几个实际的文件目录,包括“crm”、“sales”、“finance”等。
  • First off we want to strip off all the ".php" extensions which I am able to do myself thanks to these posts.首先,我们要去掉所有的“.php”扩展名,多亏了这些帖子,我才能自己做。 However, the querying of the company and contact IDs are where I am stuck.但是,公司和联系人 ID 的查询是我遇到的问题。
  • Right now if I load www.domain.com/crm/companies.php it displays all the companies in a list.现在,如果我加载 www.domain.com/crm/companies.php,它会在列表中显示所有公司。
  • If I click on one of the companies it uses javascript to call a "goto_company(x)" jQuery script that writes a form and submit that form based on the ID (x) of the company.如果我单击其中一家公司,它会使用 javascript 调用“goto_company(x)”jQuery 脚本,该脚本会编写一个表单并根据公司的 ID (x) 提交该表单。 This works fine and keeps the links clean as all the end user sees is www.domain.com/crm/company.php.这工作正常并保持链接干净,因为所有最终用户看到的是 www.domain.com/crm/company.php。 However you can't navigate directly to a company.但是,您不能直接导航到公司。
  • So we added a few lines in PHP to see if the POST is null and try a GET instead allowing us to do www.domain.com/crm/company.php?companyID=40 which displays company #40 out of the database.因此,我们在 PHP 中添加了几行以查看 POST 是否为 null 并尝试 GET 而不是允许我们执行 www.domain.com/crm/company.php?companyID=40 显示数据库中的公司#40。
  • I need to rewrite this link, and all other associated links to www.domain.com/crm/company/40我需要重写此链接以及指向 www.domain.com/crm/company/40 的所有其他关联链接
  • I've tried everything and nothing seems to work.我已经尝试了一切,但似乎没有任何效果。 Keep in mind that I need to do this for "contacts" and also on the sales portion of the app will need to do something for "deals".请记住,我需要为“联系人”执行此操作,并且在应用程序的销售部分也需要为“交易”执行一些操作。

To summarize here's what I am looking to do:总结一下我想要做的事情:

  1. Change www.domain.com/crm/dash.php to www.domain.com/crm/dash将 www.domain.com/crm/dash.php 更改为 www.domain.com/crm/dash
  2. Change www.domain.com/crm/company.php?companyID=40 to www.domain.com/crm/company/40将 www.domain.com/crm/company.php?companyID=40 更改为 www.domain.com/crm/company/40
  3. Change www.domain.com/crm/contact.php?contactID=27 to www.domain.com/crm/contact/27将 www.domain.com/crm/contact.php?contactID=27 更改为 www.domain.com/crm/contact/27
  4. Change www.domain.com/sales/dash.php to www.domain.com/sales/dash将 www.domain.com/sales/dash.php 更改为 www.domain.com/sales/dash
  5. Change www.domain.com/sales/deal.php?dealID=6 to www.domain.com/sales/deal/6将 www.domain.com/sales/deal.php?dealID=6 更改为 www.domain.com/sales/deal/6

(40, 27, and 6 are just arbitrary numbers as examples) (40、27 和 6 只是作为示例的任意数字)

Just for reference, when I used the generateit.net/mod-rewrite site using www.domain.com/crm/company.php?companyID=40 as an example, here is what it told me to put in my .htaccess file:仅供参考,当我以 www.domain.com/crm/company.php?companyID=40 为例使用 generateit.net/mod-rewrite 网站时,它告诉我要放入 .htaccess 文件的内容如下:

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^crm/company/([^/]*)$ /crm/company.php?companyID=$1 [L]

Needless to say that didn't work.不用说那没有用。

OK here is an updated based on the help received from Gohn67 below好的,这是根据下面从 Gohn67 收到的帮助进行的更新

It is working with the exception of a small bug I can't seem to figure out.除了一个我似乎无法弄清楚的小错误外,它正在工作。 I have created the .htaccess file in the "crm" directory.我在“crm”目录中创建了 .htaccess 文件。 Here is the code:这是代码:

RewriteEngine On
RewriteRule ^test/([\d]+)$ /crm/company.php?companyID=$1 [L]

This rewrites www.domain.com/test/40 to www.domain.com/crm/company.php?companyID=40 so it's very close to what I need.这将 www.domain.com/test/40 重写为 www.domain.com/crm/company.php?companyID=40 所以它非常接近我需要的。

The bug is that I cannot replace "test" with the word "company" in my RewriteRule.错误是我无法在我的 RewriteRule 中用“公司”一词替换“测试”。 I do not know why.我不知道为什么。 I can put anything but the word "company" in there;除了“公司”这个词,我可以放任何东西; even the names of other PHP files in the "crm" directory such as "contact" or "add-contact".甚至“crm”目录中其他 PHP 文件的名称,例如“contact”或“add-contact”。 As a further test I actually renamed company.php to test.php and changed the RewriteRule to:作为进一步的测试,我实际上将 company.php 重命名为 test.php 并将 RewriteRule 更改为:

RewriteRule ^company/([\d]+)$ /crm/test.php?companyID=$1 [L]

which worked.哪个有效。

Yeah, the generated rewrite looks kind of strange there.是的,生成的重写看起来有点奇怪。 I'm not sure what it is trying to match here ([^/]*) .我不确定它在这里试图匹配什么([^/]*)

Here is an example that may work for you.这是一个可能对您有用的示例。 I tested these on my system.我在我的系统上测试了这些。

RewriteEngine On
RewriteRule ^crm/dash/?$ /crm/dash.php [L]
RewriteRule ^crm/company/([\d]+)/?$ /crm/company.php?companyID=$1 [L]
RewriteRule ^crm/contact/([\d]+)/?$ /crm/contact.php?contactID=$1 [L]

This is only a few of your routes as an example.这只是您的一些路线作为示例。 I admit that they could be more robust though, because doing this way will lead to a lot of rewrite rules, some of which you could elminate with better regex patterns.我承认它们可能更健壮,因为这样做会导致大量重写规则,其中一些您可以使用更好的正则表达式模式来消除。 But hopefully this gets you started.但希望这能让你开始。

Here are some updated rewrite rules taking into consideration a subdirectory.以下是一些考虑到子目录的更新重写规则。 It also fixes aa mistake from above:它还修复了上面的一个错误:

RewriteEngine On
RewriteBase /crm
RewriteRule ^dash/?$ dash.php [L]
RewriteRule ^company/([\d]+)/?$ company.php?companyID=$1 [L]
RewriteRule ^contact/([\d]+)/?$ contact.php?contactID=$1 [L]

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

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