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
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
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]
Related
I'm trying to learn, so please be kind. My site is database driven and uses ID numbers to generate the pages. I'd like the URL not to show the ID number but the name of the page. Is there a way to do this where it is just cosmetic and doesn't effect the site? I'm also most likely screwing up the way I'm writing the redirect as it doesn't seem to work at all? What am I doing wrong? Thank you.
This is how my URL looks now:
http://mydomainname.com/index.php?id=35-Entertainment
I'd like it to look like this:
http://mydomainname.com/Entertainment
RewriteEngine On
RewriteRule ^([^/d]+)/?$ index.php?id=$1 [L,QSA]
Unfortunate there is not a way to do this.
The process in this case is the following:
A user enters the a url. In this case you want a nicely formatted URL (http://mydomainname.com/Entertainment)
The user sends this url to your server
Apache processes the url and points it to your project directory based on the domain used in the url
There Apache uses the htaccess to see what to do next. In this case the best method would most likely be to use RewriteRule ^(.*)$ index.php?url=$1 [QSA,L] or something like that
Then you need to do the routing within your app. You can use a routing table for instance to map the nicely formatted slug to an module/id combination
This process can't be done by Apache for it does not known about the content present in your database.
Hopefully this is of any help for you.
I need to do this using htaccess
When a request is made for http://www.example.com/home, it should (internally)load the page http://www.example.com/home_st.php
Similarly when a request is made to other link called products (http://www.example.com/products), it should (internally)load http://www.example.com/products_st.php
In short what it is doing is appending "_st.php" and loading that URL. But one thing I do not want is if the user directly types http://www.example.com/home_st.php or http://www.example.com/products_st.php in the browser, it should show 404 / Page not found error
I have few other pages in that folder and I want those pages to behave in this manner. I understand the htaccess should have something like this
Turn on the rewrite
Forbid access if the URL is called with page names like home_st.php, products_st.php etc.
If it's "home" or "products", then rewrite(append?) it to home_st.php and products_st.php respectively. I have other files too while need to follow the same
P.N: My URL should not show the actual filename, for example home_st.php, products_st.php etc. It should only show as http://www.example.com/home, http://www.example.com/products etc
htaccess and regex is not something that I am well acquainted with. Any help would be great. Thanks
You want to be able to re-write URL's
This has been written before but i'll say it again.
You want to use the Htaccess file and the re-write rule.
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^pet-care/?$ pet_care_info_01_02_2008.php [NC,L] # Handle requests for "pet-care"
This will make this url: http://www.pets.com/pet_care_info_07_07_2008.php
Look like this: http://www.pets.com/pet-care/
Links to more information: How to make Clean URLs
and for the webpage I used to reference this information from: https://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/
To get your pages set the way you want them, I would advise that you read the two articals and try get what you are looking for.
If you need some specific help with doing this, ask.
Hope that helps.
Im trying to write a redirect rule with mod_rewrite, but somehow it never functions, so i must be doing something wrong :(
The below is the rewrite rule as i've defined it in an .htaccess file, it should be noted that there's another block above this one, that comes from WordPress, i tried combining that one and the below into 1 rewrite rule to see if that was maybe the issue, but that also didn't fix things. Anyway, this is what i have:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} =facebookexternalhit\/[0-9]+(\.[0-9]+)*
RewriteRule ^/hash/([A-Za-z0-9-]+)/?$ \#$1 [R]
</IfModule>
The RewriteCond part checks if the request comes via Facebook (kept out of the equation so far during my testing of the RewriteRule) i'm not sure if this actually works, but i came across it in multiple StackOverflow questions, so i'm assuming that's fine (honestly it doesn't really matter, and i may omit that condition all together, just posted here in-case it turns out to be invalid)
The RewriteRule part should redirect all url's containing /hash/ too url's containing #, so what this means is that http://www.example.com/hash/home should redirect the browser too http://www.example.com#home
I've tried a load of different iterations of the above, but whatever i try, it simply doesn't appear to function, hence this question, does anybody see anything wrong with the code? would it be an issue that there are multiple blocks defined in 1 .htaccess file?
Finally, is this even a valid solution? the actual underlying problem is this:
I have a site that uses hash-tags to navigate to certain pages, in the site there are some sharing buttons that share the current url a user is looking at, Twitter LinkedIn and Google+ all handle the shared url's (containing a hash-tag) without issue, but Facebook strips the hash-tag from the url in the sharer (if send it along as # hash-tag, if i try send it along as url encoded %23 hashtag Facebook reports a 404 on the url), so i need to make a workaround for Facebook where i can share a url without a hash-tag with Facebook, and then redirect those url's to something actually understood by the website when a visitor or the Facebook sharing api 'comes knocking'.
Any help highly appreciated!
So I did quite a bit of research, but since I'm very new to manipulating .htaccess, I couldn't really find a good answer.
This is my site structure:
www.kanadax.ca/index.php is the landing page. This is what Guest users (in Joomla speak) see when they land on the page, before they signup.
www.kanadax.ca/index.php/anasayfa-2 is the home page for group 1 users, aka non-paying registrants.
www.kanadax.ca/index.php/anasayfa-3 is the home page for group 2 users, aka paying registrants.
Now ideally, I would like to make all of these just be www.kanadax.ca/ in the URL bar. The server should know which one to send the user to, but the end user should just see www.kanadax.ca/. I'm not quite sure if this is possible, so this is my first question. If so, what should the .htaccess look like?
If this isn't possible, can I at least take out index.php from each? So they would be
www.kanadax.ca/
www.kanadax.ca/anasayfa-2
www.kanadax.ca/anasayfa-3
Now of course, anasayfa-2 and -3 are not good practice (anasayfa=home) so I would be very thankful if someone can walk me through removing those if it's not possible to simply make all of these be displayed as www.kanadax.ca/
I hope I was able to explain things clearly but please don't hesitate to ask questions.
This is usually solved by enabling Use URL Rewriting and Search Engine Friendly URLs within the Global Configuration settings for Joomla. Be sure you have a Joomla .htaccess file aswell. There should be a provided htaccess.txt file you can copy to .htaccess if you don't. Additionally, confirm your Joomla cache has been cleared and your browser cache has been cleared before attempting to check for changes.
you can redirect your index.php page to your domain
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php\ HTTP/
RewriteRule ^(.*)index\.php$ http://www.kanadax.ca/$1 [R=301,L]
althogh you cannot redirect user to a specific page because info are stored in joomla session and you cannot make any rule or condition in htmlaccess basing on that.
1- Does mod_rewrite means that if I make this url:
domain.com/ad.php?id=8498292
INTO
domain.com/8498292
that all links on my website will have to be changed to the later above?
example the link: domain.com/ad.php?id=8498292 wont work now, unless I replace it with domain.com/8498292 ?
Or will the server know that they are the same still?
2- Will the rewritten link appear rewritten in the browsers adress bars also, so if I enter domain.com/ad.php?id=8498292 it will actually appear as domain.com/8498292 in the adress bar itself?
3- Will images and all other related links and material on the page whose link is rewritten remain intact? ie will pictures and links still work FROM that page which are relative?
Thanks
You can write the rules such that both will work, but generally you'll want the links to be in the "clean" format for when search engines index your pages.
mod_rewrite can do an internal rewrite if the pages are on the same domain. One would have to use the [R] flag to force an external redirect if that was desired.
You can make the rules as expansive or as restricted as necessary in order to avoid rewriting media URLs. RewriteCond has a number of ways to test the viability of rewriting.