简体   繁体   English

.htaccess url重写白标签网站

[英].htaccess url rewrites for white label sites

I'm building a simple site that will only have a homepage and a contact page and wondered if I could use .htaccess to rewrite the urls for different companies. 我正在建立一个只有主页和联系页面的简单网站,并想知道我是否可以使用.htaccess重写不同公司的网址。

So for example if I go to website.com/companyname-contact/ it will display that url in the browser bar but actually load a generic contact.php page, I can then use php to pull in the correct contact details for that specific companyname. 因此,例如,如果我访问website.com/companyname-contact/,它将在浏览器栏中显示该URL但实际上加载了一个通用的contact.php页面,然后我可以使用php获取该特定公司名称的正确联系详细信息。

This would need to work for different company names (eg website.com/anothercompany-contact/) but only work for an array of approved company names. 这需要适用于不同的公司名称(例如website.com/anothercompany-contact/),但仅适用于一系列经批准的公司名称。

I realise this may not be possible but I thought I'd ask because i spent about 4 hours this morning Googleing it with no real progress. 我意识到这可能是不可能的,但我想我会问,因为我今天早上花了大约4个小时谷歌它没有真正的进展。

Thanks 谢谢

Unless you want to manually list the approved company names in your .htaccess file (which looks UGLY ) I'd suggest this: 除非您想在.htaccess文件中手动列出已批准的公司名称(看起来很丑陋 ),否则我建议:

RewriteEngine On 


  RewriteRule (.*)-contact$ /contact.php?company_name=$1 [L,QSA,NC]

and then in your contact.php 然后在你的contact.php中

  • determine if valid company name - check db or whatever method you are using. 确定有效的公司名称 - 检查db或您正在使用的任何方法。 (Make sure to escape the input) (确保逃避输入)
  • if not valid you have a couple options: 如果无效,您有几个选择:
    • redir to your default 404 page redir到您的默认404页面
    • issue an intelligent warning page (ie include suggestions for alternate spelling that is in the db) and set a 404 header. 发出智能警告页面(即包括数据库中备用拼写的建议)并设置404标题。 ( better IMO ) 更好的IMO
    • if similar company name in the db possibly redirect to that with a note at the top of the page 如果数据库中的类似公司名称可能重定向到页面顶部带有注释的公司名称

Yes you can. 是的你可以。 You need to enable the rewrite engine, and then you will be able to use regular expressions to accomplish what you're trying to do. 您需要启用重写引擎,然后您将能够使用正则表达式来完成您尝试执行的操作。

This is an example of what your htaccess could like: 这是你的htaccess可能喜欢的一个例子:

RewriteEngine On
RewriteRule    ^contact/([A-Za-z0-9-]+)/?$    contact.php?company=$1    [NC,L]

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

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