apache mod_rewrite: using database to update rewrite rules - apache

Total newbie at mod_rewrite.
Let's say I want to create nice URLs for every manufacturer on my site,
so I have
www.mysite.com/samsung
www.mysite.com/sony
www.mysite.com/acme
works well enough.
However, if I have hundreds of manufacturers and if they're changing constantly, what then? There are some vague references for something called rewrite map somewhere but nothing that explains it and no tutorials. Can anyone help?
Also, why is this problem not the main topic covered in tutorials for mod_rewrite? How is mod_rewrite possibly useful when you have to maintain it manually (assuming you have new content on your site once in a while)?
There is also mention of needing to have access to httpd.conf
How do I access httpd.conf on my hosting provider's server? How does every other site do this?
Thanks

Just came across this answer while searching for a similar solution — searching a bit further I discovered that mod_rewrite now has the RewriteMap directive, which will do exactly what you want without the need to run PHP or another scripting language.
It lets you define a mapping rule with a text file, a DBM file, an external script or an SQL query.
I hope that helps!

The way this would typically be done is that you would take all URLs that match a specific pattern and route them to a PHP file (or whatever your server-side programming language is) for more complex routing. Something like this:
RewriteRule ^(.*)$ myroute.php?url=$1 [QSA,L]
Then, in your myroute.php file, you can include logic to look at the "url" query string parameter, since it will contain the original URL that came in. Perhaps you could match it to a manufacturer in the database, or whatever else is required.
This example obviously takes all URLs and maps them to myroute.php. Another example might be something like:
RewriteRule ^/manufacturers/(.*)$ manuf.php?name=$1 [QSA,L]
In this case, it will map URLs like so:
/manufacturers/sony => /manuf.php?name=sony
/manufacturers/samsung => /manuf.php?name=samsung
etc...
In this case, your manuf.php file could look up the database based on the name query string parameter.

Related

How to direct multiple clean URL paths to a single page?

(Hi! This is my first time asking a question on Stack Overflow after years of finding answers here... Thanks!)
I have a dynamic page, and I'd like to have fixed URLs that point to different states of that page. So, for example: "www.mypage.co"(/index.php) is the base page, and it rearranges its content based on user choices. I'd then like to be able to point to "www.mypage.co/contentA" or "www.mypage.co/contentB" in order to automatically load base the page at "www.mypage.co" with the desired content.
At heart the problem is an aesthetic one. I know I could simply write www.mypage.co/index.html?state=contentA to reach the desired end, but I want to keep the URL simple and readable (ie, clean). I also, due to limitations in my hosting relationship, would most appreciate a solution that is server-independent (across LAM[PHP] stacks, at least), if possible.
Also, if I just have incorrect assumptions about how to implement clean URLs, I'd appreciate direction to a good, comprehensive explanation. I can't seem to find one...
You could use a htaccess file to redirect all requests to one location and then from there determine what you want to return to the client. Look over the htaccess/dispatch system that Tonic uses.
If you use Apache, you can use mod_rewrite. I have a rule like this where multiple restful urls all go to the same page, using regex and moving parts of the old url into parameters for the new url:
RewriteRule ^/testapp/(name|number|rn|sid|unii|inchikey|formula)(/(startswith))?/?(.*) /testapp/ProxyServlet?objectHandle=Search&actionHandle=drillIn&searchtype=$1&searchterm=$4&startswith=$3 [NC,PT]
That particular regex accepts urls like
testapp/name
testapp/name/zuchini
testapp/name/startswith/zuchini
and forwards them to the same page.
I also use UrlRewriteFilter for Tomcat, but as you mentioned PHP, that doesn't seem that it would be useful.

Disallow double or junk wildcard subdomains in htaccess mod rewrite for SEO

I have wildcard subdomains enabled on my domain. I use this so that I can rewrite urls like es.domain.com to domain.com/page.php?lang=es and display to the user the local language version of page.php.
The one potential problem I see with allowing wildcard subdomains is that people can link to www.es.domain.com or even anything.they.like.domain.com and it will display a perfectly working clone of the website. I presume this 'duplicate content' is bad for SEO.
Can anyone come up with a RewriteRule which detects subdomains of more than 2 letters (www. excluded of course) and 301 redirects offending urls to the clean base domain.com? I'm having trouble when I consider domains like domain.co.uk which already look like they are on a subdomain.
As a side note, are there any similar implications for SEO on the opposite side of the url, with query parameters? For example, domain.com?param=anything-I-like will surely show a duplicate page. How does Google handle this content?
UPDATE:
Here's the rewrite rule I'm using currently. If I wanted to clean up bad urls with PHP, I'd need to modify this to catch all subdomains. i need to do this generically (without specifying domain.com) as it's going to be used on a CMS. Any suggestions?
RewriteCond %{HTTP_HOST} ^([a-z]{2})\.
RewriteRule p/(.*) page.php?p=$1&lang=%1
I honestly can't speak to fixing your actual issue, but I can confirm that anything.I.want.domain.com is really, REALLY bad for SEO. I've got two years' experience in the field and I'm currently working on a project cleaning up links for our main U.S. site. A couple of the biggest problems have come from sites just like you described where there were around 100 *.domain.com. The biggest issue is the effect of this problem with trust flow, it basically sends a link's trust rating to 0 and tells Google that, not only should this link be disregarded, the domain it came from and links to should be investigated for potential spammy-ness.
As to your final question on implications:
Query parameters can be just as helpful or detrimental as any other URL structure, so you want to be careful with those, as well. If you've got different language versions of your site, be sure to have one (especially if you don't have entirely unique content) as the rel-canonical page. The thing is, linking structure is important to search engines, but not overly so. It's one of many metrics. I'd be far more concerned about the subdomains. If you happen to be able to sneak in some small, basic keywords that help describe the page in with your query vars, it could help a bit. I would, however, highly suggest that you have a three or four tiered structure to your site, supported in the URLS.
See this
Google tends to like: domain.com/landingpage/category/subcategory?somevars=44
Going more than three deep spreads you too thin and less than that makes the site too bulky to navigate. I believe it's covered somewhat here if you've never seen it: http://moz.com/beginners-guide-to-seo
Search Engine Journal
Single Grain and
Moz
can answer a lot of your SEO questions and tools like:
Majestic
Soolve
Mozcast
SERPMetrics Flux
can help a lot, too. Try doing a little reading and see if you can decide a good scheme for your links.
Again, sorry, I don't know really any Apache, but hopefully that'll help!
Presumably you have a rewrite rule that takes anything in front of domain.com and puts it into the lang parameter. Rather than having a rewrite rule to do the redirecting, have your page.php script examine the lang parameter and issue a redirect for invalid values.
Thanks to all for the info & replies on this. The solution I've found is to write a more generic .htaccess rule to catch all subdomains and forward them to PHP for processing. PHP then checks if the subdomain is valid and if not, 301 redirects the visitor to the root domain. This way if someone links to blah.blah.domain.com, Search engines should see that as a link to just domain.com. I'm only using language subdomains on my site but it should work for any subdomains you want to use.
Here's the htaccess rewrite:
The regex works by finding the last instance of more than 3 domain-name-valid characters, followed by a dot, followed by any other string. The idea is that it finds the domain name in the url, then captures everything before it. Obviously this wont work for domains which are shorter than 3 characters.
#All sub domains are redirected to p.php for processing:
RewriteCond %{HTTP_HOST} ^(.*)\.[a-z0-9\-]{3,}\..*
RewriteRule (.*) p.php?subdom=%1 [L]
Here's the PHP:
function redirect301($page='/'){
header("HTTP/1.1 301 Moved Permanently");
header("Location:{$page}");
exit();
}
$subdom = $_REQUEST['subdomain']; //you should sanitise this if using this script!
$defaultLang = 'en';
$alternateLangs = "de|es"; //list of allowed subdomains
$alternateLangs = explode('|',$alternateLangs);
if(!empty($subdom) && $subdom!= 'www'){
if( !in_array($subdom,$alternateLangs) ) redirect301(); //redirect to homepage
$ISOlangCode = $subdom; // en,es,de,etc - capture code for use later
}
if($defaultLang && $ISOlangCode == $defaultLang) redirect301(); //disallow subdomain for default language (redirect to homepage)
Hopefully this helps someone out.

htaccess redirect rule help please (wildcard in URL)

I'm rebuilding a site from the ground up, but the site I'll be replacing already ranks pretty well for SEO.
I have a number of pages in the format of the following:
http://URL/SECTION/ANOTHERSECTION/send-me-information-on-PRODUCTNAME.php
"send-me-information-on-" is consistent across all products.
I can write redirects on a per product basis, but I've got more than 200 products so it would be great to handle this using a rewrite rule.
What I need to achieve is the following New URL:
http://URL/SECTION/ANOTHERSECTION/product-information-request.php?product=PRODUCTNAME
Now I understand for SEO purposes, this probably isn't the best approach, but I'd like to maintain a single information request page.
I figured the best approach would be to use a Regex to match the string, and set an environment variable which I'd use in the resulting URL. I'm not too familiar with .htaccess rules though.
Can anyone help me achieve this?
RewriteRule ^/([^/]+)/([^/]+)/send-me-information-on-([^.]+).php$ $1/$2/product-information-request.php?product=$3 [QSA,L]

Simple mod_rewrite, replace one word in every instance

I've been looking for an answer to this forever and can't find it, yet it seems like it should be so simple!
I want to use mod_rewrite to replace a word in a url in every instance that it shows up, but I don't want a redirect to happen, just changing the way the url appears to site users.
Example:
Change
mysite.com/something/groups/anything...
to:
mysite.com/something/projects/anything...
I know I could go through and start tweaking files but mod_rewrite would work much better because I'm sure I'll mess something up otherwise (for reference I'm using joomla/jomsocial).
RewriteEngine On
RewriteRule something/groups/.*$ something/projects/$1

Dynamic URL Rewriting with IIS6

I've been tasked with making an already existing e-commerce site SE friendly - which in this case means (amongst other things) letting the user change the URL for each page/product through the back end.
The site is an old asp site running on IIS6. I've started looking into http://www.codeplex.com/IIRF and http://www.helicontech.com/isapi_rewrite/ , but I'm a bit dubious about how to let the user change the URLS without them going into the server and hard coding them.
Ionic's Isapi Rewrite Filter runs from a .ini file, so I'm thinking that I will get the back end of the site to write to this ini file based on form inputs.
Does anyone have any experience or advice with regard to this?
edit:server is dedicated
By change URL fro each page, I think you mean change slug, or do you actually mean URL.
Slug: www.somesite.com/products/{slug}
URL: www.somesite.com/{url}
Here is how I would do it.
Give an original structure like this:
www.somesite.com/products.aspx?id=23
with an end goal of it to look like
www.somesite.com/products/the-product-to-be-sold
Or better yet
www.somesite.com/products/23/the-product-to-be-sold
I would create a rule that looks like this.
RewriteRule /products/([0-9]+)/(.*) /products.asp?id=$1&slug=$2 [NC]
That way you don't have to change anything, the name is in the URL for SEO optimization, and the ID is still there too.