0

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.

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.

This would need to work for different company names (e.g. website.com/anothercompany-contact/) but only work for an array of approved company names.

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.

Thanks

user1156756
  • 157
  • 2
  • 3
  • 10

2 Answers2

2

Unless you want to manually list the approved company names in your .htaccess file (which looks UGLY) I'd suggest this:

RewriteEngine On 


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

and then in your contact.php

  • determine if valid company name - check db or whatever method you are using. (Make sure to escape the input)
  • if not valid you have a couple options:
    • redir to your default 404 page
    • issue an intelligent warning page (ie include suggestions for alternate spelling that is in the db) and set a 404 header. (better IMO)
    • if similar company name in the db possibly redirect to that with a note at the top of the page
Nick Dickinson-Wilde
  • 1,015
  • 2
  • 15
  • 21
  • I disagree on the HTTP response, actually. Whenever you are using server-side logic to handle server responses, it is better to impose the HTTP standard because a) it helps prevent how the internal logic of the site works, b) it makes the response codes actually meaningful (as a standard), and c) it makes bot-crawling and potential API's more consistent and ready to roll. Imagine if someone cross-linked to a company on their external blog but spelled the name wrong. It would be better if search engines hit a 404 and dismiss rather than index a warning page. Maybe the 404 goes to a custom page. – Anthony Mar 04 '14 at 15:05
  • @Anthony By warning I was meaning a warning page with specific comment on the situation (like suggestion of a matched similar spelling or auto-redirecting to the similar spelling) but *with a 404 status code* rather than a generic whole site 404 page – Nick Dickinson-Wilde Mar 04 '14 at 15:07
  • 1
    ah, so custom 404. I'd personally not offer suggestions, just for security, but that's not a deal breaker. Just wanted to speak up regarding good HTTP responses. a lot of people think 404 should always be avoided, which is wrong. or think they should use 404 when they mean 403 or 410, etc. I'm a little teapot is also not used often enough. – Anthony Mar 04 '14 at 15:16
  • 1
    Thanks very much @nickwilde. Your solution works perfectly. Not sure how I'm going to read the allowed company name yet. I'll probably do it via DB or an xml file. – user1156756 Mar 05 '14 at 09:30
0

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:

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