Very simple mod_rewrite questions - php

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.

Related

htaccess change requested filename

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.

.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.
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]

Keep old website (HTML files) on webserver but disallow search agents to index them

I’ve just finished a website for a client who is going to replace their old (very old, HTML hard-coded website). The problem is that they (for now) want to save their old website and all the files on the webserver in the original position. This does not create any issues with the new website which is made in PHP and Wordpress but it makes a big deal when Google (and others) are dropping by with their search robots and indexing.
When doing a Google search it still finds the old HTML files. Is there any way that I could “keep” the old HTML files on the web server but make sure that for the first no robots are going to index them and if anyone is trying to navigate to an HTML page, e.g. http://www.clientdomain.com/old_index_file.html, they are getting redirect? I think the last part might be able to be done in .htaccess but I haven’t found anything useful searching for it.
The first question about not allowing robots and agents to index HTML files, I’ve tried to put these two lines in my robots.txt file
Disallow: /*.html$
Disallow: /*.htm$
But I’m unsure if it will work?
I might deal with this in a completely wrong way but I’ve never tried that a client has requested to keep the old website on same server and in original location before.
Thanks,
- Mestika
<?php
$redirectlink = ‘http://www.puttheredirectedwebpageurlhere.com‘;
//do not edit below here
header (‘HTTP/1.1 301 Moved Permanently’);
header(‘Location: ‘.$redirectlink);
exit;
?>
This code will use a 301 redirect the page to the URL that you desire. The filename of this .php should be the URL slug of the page you want to redirect.
301 Redirect
A 301 redirect, or also known as a permanent redirect, should be put in place to permanently redirect a page. The word ‘permanent’ is there to imply that ALL qualities of the redirected page will be passed on to the detour page.
That includes:
PageRank
MozRank
Page Authority
Traffic Value
A 301 redirect is implemented if the change you want to make is, well… permanent. The detour page now embodies the redirected page as if it was the former. A complete takeover.
The old page will be removed from Google’s index and the new one will replace it.
Or you can do it in your htaccess like shown by the above poster.
There's probably a lot of ways to handle this, assuming you have a clear mapping of pages from the old template to the new one, you could detect the Google bot in your old template (see [1]) and do a 301 redirect (see [2] for example) to the new template.
List item
[1] how to detect search engine bots with php?
List item
[2] How to implement 303 redirect?
Will take some work, but sounds like you'll need to crack open your htaccess file and start adding 301 redirects from the old content to the new.
RewriteCond %{REQUEST_URI} ^/oldpage.html
RewriteRule . http://www.domainname.com/pathto/newcontentinwp/ [R=301,L]
Rinse and repeat
This is definitely something mod_rewrite can help with. Converting your posted robots.txt to a simple rewrite:
RewriteEngine on
RewriteRule /.*\.html /index\.php [R]
The [R] flag signifies an explicit redirect. I would recommend seeing http://httpd.apache.org/docs/2.4/rewrite/remapping.html for more information. You can also forbid direct access with the [F] flag.

Redirect url for public page in wordpress?

I am working on this site developed in Wordpress 3.3.1. My client wanted me to develop a public page. This page will be a simple php page. It will reside in wordpress directory but will not be a part of CMS itself. The issue here is that I want a url redirection for this page. My client wants to send a link in email to members of the website, so he wants to keep the url clean.
This is the format of current url that he wants to send to the members:
'http://www.example.com/shop/"url_encoded_category_name"/product/"product_id"'
And I want to redirect it to:
'http://www.example.com/template-public-home?productId="product_id"'
I was rewriting the url for now. But as you can see in the first url format that "url_encoded_category_name" and "product_id" are variables and therefor rewriting would mean that I am trying to rewrite different urls to same url(only query string changes). I want to change it to redirection because as I understand this approach of rewriting multiple urls to the same url is penalized by most of the search engines.
For rewriting I edited the .htaccess file.
What I wanted to ask is that considering that I have a publicly accessible page within wordpress directory:
what is the most suitable way to redirect my url?
In my case what is a better Code? 301 or 302?
Thanks for any assistance you can provide or direct me to a source where I could learn about it.
First of all, if your client just wants to use the generated URL in emails to members, no search engine will ever know. However, if there is a chance that they leak you might indeed end up with duplicate content. Redirection then is the correct approach.
You can redirect just as you rewrite by using the [R] flag in your rule (usually in conjunction with L as [R,L] so that the rules below that match are not executed.
From the point of view of a search engine (and a user as well), these are permanent redirects - you will never ever use the URL in the email as a primary URL (or will you?). That means you should use R=301.
Take a look at the documentation to learn about the flags, test your rewrite rules online here and check https://stackoverflow.com/questions/1426056/good-htaccess-mod-rewrite-url-rewriting-tutorial for some hands-on material.

How to make extension-less url for a PHP based site?

Do I have to put every file in a different folder?
like:
about-us/about-us.php
profile/profile.php
etc.
or is there any other automatic solution.
I want to convert
http://sitename.com/about-us/about-us.php
to
http://sitename.com/about-us
You want pretty URL rewriting.
An Apache .htaccess examples from that article:
Pretty URL: /browse/animals-24/cats-76.html
Ugly URL: /browse.php?category=24&subcategory=76
.htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^browse/[A-Z0-9_-]+-([0-9]+)/[A-Z0-9_-]+-([0-9]+)\.html$ browse.php?category
If you have a directory called about-us, then you could simply create an index.php file within that directory, and by default, your .htaccess file should redirect users to the correct page.
Thus, going to example.com/about-us/ would bring the user to the same page as about-us.php. It would benefit you to do some research about 301 redirection.
http://www.tamingthebeast.net/articles3/spiders-301-redirect.htm
Basically, when Googlebot crawls your website, the last thing you want is for Google to find both copies of the page, one listed as about-us/ and one listed as about-us/about-us.php. Duplicate content is bad, and optimizing your website for search engines is really not at all that difficult to do.
Let's say you have a leaderboard page on your website with 1000 members. Instead of Google finding all leaderboard.php?user=Whatever, it would be a good idea to block that page from being accessed by Google, or else you will result in hundreds of unwanted archived pages on their search engine.
You might also want to make sure your website can be accessed either by www.yourwebsite.com or by simply yourwebsite.com, BUT NOT BY BOTH (without being 301 redirected).
Hope that helped. Happy programming!
EDIT: If you try renaming your about-us.php file to simply index.php, that would be your quick fix. Depending on your .htaccess configuration, I'm willing to bet that would be your easy fix.

Categories