Prevent users browsing the website with the old URL after rewriting - php

I am new to .htaccess, and I am trying to make friendly URLs (or pretty, SEO-friendly URLs). I did the following things on .htaccess:
- Remove .php extensions on all the file.
- Rewrite the product details pages as follow:
RewriteRule ^product-details/([a-zA-z0-9_-]+)/([0-9]+)$ product-details.php?title=$1&id=$2 [L]
However, when I go to the URL bar and type the file name with the .php extension, it still works. I want it to redirect the user to the 404.php page. Also, with the product details page, I type the old URL format, it also works.
Illustrations:
index => index.php (Works)
product-details/chair/1205 => product-details.php?title=chair&id=1205 (Works)
How to prevent this? Should I use PHP or .htaccess? If so, what is the .htaccess solution? Thanks a lot!

After RewriteRule, old URL's will automatically redirect to new URL's. Like if the user will open product-details.php?title=chair&id=1205 he will automatically redirect to product-details/chair/1205
So old .php extension URL's will never work even if someone will try to open them.
And I don't recommend to send old URL's to 404 page as this will make wrong impression on Users and not good for SEO.

Related

how to find redirect source through htaccess in php?

I have a site with lots of links that redirect to a small set of links.
I used the htaccess redirect 301 to redirect a user going to www.example.com/something/something_else/ , to www.example.com/my/collective/url/index.php where something and something_else are not actual folders but just keywords.
Is there a way in index.php to know which url it was redirected from?
I tried $_SERVER['HTTP_REFERER'] but it gave me nothing.
EDIT:
the rewrite rules I used are as follows:
RewriteRule ^something/something_else/(.*)$ /my/collective/url/$1 [R=301,L,NE,P]
UPDATE:
I eventually added a php script for the rewrite, in order to get the HTTP_REFERER. Seems there is no way to properly get it, since after redirect the HTTP_REFERER turns to the redirected url or disappears entirely.
Would still be happy to get an answer though if there is a way.
You can use $_SERVER['REQUEST_URI'] this will not be affected by a RewriteRule, but will not do any good no a pure redirect.
Instead of using a line like so:
RewriteRule ^.*$ index.php [R=301,L]
You could remove the R=301 part to not change the page location viewed by the browser, that way, the user thinks he really landed on your page using the /something/something_else link. This is also perfect for SEO... Just look out to not create too many links pointing to the same content...

Troubles Overwritting Wordpress Url Rewrites

so essentially right now I have a plugin enabled to allow me to use php in my pages. I've setup a page and set the permalink as the homepage. Doing this I'm able to make requests like
http://mysite.com/?profile=Rihanna
and the php code will execute based on the given artist.
The issue appears when I try to make this url more seo friendly, I add the following rewrite rule to my .htaccess
RewriteRule ^artist/(.*).html http://mysite.com/?profile=$1
thinking that it should work but it does not, it simply goes to the wordpress 404 page. Now if I redirect it to a regular html file instead of mysite.com/?profile=$1 it works just fine.
Does anyone have any idea what's wrong and how I can get it working properly?
Thanks
Make sure your rule is before all of your wordpress rules. Wordpress' rules route all requests through am index.php and since "artist" is probably a bad permalink, it'll return a 404.
Additionally, if you include the http://mysite.com in your rule's target, it will inherently redirect the browser as opposed to internally rewriting the URI. You should remove it and include an [L] flag so rewriting stops in the current iteration (thus the wordpress rules won't get applied):
RewriteRule ^artist/(.*).html /?profile=$1 [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.

.htaccess & wordpress site - How to add 301 redirect rules for no longer valid permalink?

I recently did a big update on my wordpress site, thus some old permalink are invlid now.
Seach google for a tutorial on this and found a lot about static html page redirection examples or specific php page which are not what i'm looking for
Both my old and new permalinks are in path format, for example, i need redirect a couple urls of old gallery posts (deleted) which are
/2009/06/gallery/abc/
/2009/06/gallery/cba/
/2009/06/gallery/bbc/
/2009/06/gallery/aab/
to a new page which is
/gallery/
How do i write a correct redirect rule for this?
RewriteRule ^[0-9]{4}/[0-9]{2}/gallery/.*$ gallery [R=permanent,L]
Should get you to where you need to be. If you need the actual parameters (IE Date / Month / Title) passed along it is a bit of a change up, but do-able.
EDIT
Fixed typo.
Try something like this:
RewriteEngine On
RewriteRule ^/2009/06/gallery/\(?([^/]*)\/$ /gallery/ [R=301,L]
Personally, I would use the Redirection plugin rather than editing your .htaccess directly -- it's easier, less danger-prone, and will let you log what redirections have been happening. It will also track 404 errors so you can see if you've forgotten to redirect anything.
Then, if you're just redirecting a couple of fixed posts, it's just a matter of adding a simple rule for each redirect, with the old and the new URLs, on the Redirection configuration page. You can also use regular expressions, as with .htaccess, if you want to do anything more complicated.

Changing WordPress URL structure while maintaing the proper 301 redirects with mod_rewrite

I currently have a blog set up with WordPress using URLs like so:
www.domain.com/blog/?pid=384092817
This was before I knew anything about anything. There's a significant amount of content there now and I want to change my URL structure to:
www.domain.com/my-post-title-384092817
Is there a way to set up my .htaccess and mod rewrite so when users go to the first url they get a 301 redirect to the second URL? I know to change my WordPress address (URL) setting from www.domain.com/blog to www.domain.com and my permalink setting to /%postname%-%post_id%, but how do I tell the old URLs to redirect to the new ones?
Do you actually mean that when users go to the second URL, it will be rewritten to the first? That can be done with
RewriteRule /blog/.+-(\d+)$ /blog/?pid=$1
If you want to send 301 redirects from the old URLs to the new ones, then you can't really do that with an .htaccess file. The reason is that Apache's mod_rewrite doesn't have access to the title of your post, so it won't know what title to insert in the URL. You'd have to do that redirect with PHP.
EDIT: y'know what, actually that's not entirely true. You can do some pretty crazy things with the RewriteMap directive, such as instructing Apache to ask an arbitrary program to perform the rewriting, and in that way you could have, say, a PHP script that does some database work to figure out what the title is and rewrites the URL appropriately based on that. But that seems like a way overcomplicated solution and I wouldn't suggest doing it - it'll be much cleaner and easier to just send the redirect directly from PHP.
Depending on your WP version, you can just use the Permalink redirect plugin -- should do the trick for you within WordPress and without mod_rewrite. However, as of WordPress 2.3, a lot of this should work automatically. At that point, the only thing you should have to do is redirect all your /blog/... requests to the route, which you can do via mod_rewrite roughly like this:
RewriteRule ^blog(.*) $1 [QSA]

Categories