Looking for guidance rewriting specific URL's - php

I have changed the CMS to my site and it has rewritten the URLs to be more friendly but I have 20 pages which are linked on different sites across the web. I want when people click those old links for them to be redirected to their new respective posts.
I was at first thinking of using a PHP array with a javascript redirect but now I'm thinking .htaccess mod-rewrite would be best.
Whats the best way to have specific URLs redirect to their new URLs?

Use the .htaccess file for redirects. For example:
# This allows you to redirect index.html to a specific url
Redirect /index.html http://example.com/location/
Add some regex to match your rewrites and send to the dynamically created remote url. For example:
# Dynamic redirect
RedirectMatch 301 yourUrl/\(.*)\$ http://external.com/someUrl.html?dynamicVal=$1
That would send ...yourUrl/123 -> http://external.com/someUrl.html?dynamicVal=123

If you are talking about URLs on your pages, then the best way is to edit all such pages with current proper URLs.
If you are talking about "what to do when someone hits my old URL (from their bookmarks or by clicking a link on another site, for example) then the best way is to redirect (with code 301) such requests to a new location using Apache's URL Rewrite module.
Example:
# Activate Rewrite Engine
RewriteEngine On
# redirect to another page on the same site
RewriteRule ^mypage.php /other/mypage.php [R=301,L,QSA]
# redirect to another page on ANOTHER site
RewriteRule ^mypage.php http://www.anothersite.com/mypage.php [R=301,L,QSA]
Here is the link to Apache's manual for mod-rewrite.
Another useful place -- Mod_Rewrite Forums.

Apache supports different modules to rewrite/redirect requested URIs:
mod_alias with Alias and AliasMatch for internal rewriting, as well as Redirect and RedirectMatch for external redirecting
mod_rewrite with RewriteRule for internal rewriting and external redirecting
The main difference between the mod_alias and mod_rewrite directives is that mod_alias’s Alias and Redirect directives work with path prefix patterns while the other directives (i.e. AliasMatch, RedirectMatch, and RewriteRule) work with regular expressions.
A path prefix pattern is always always matched when it’s a qualified prefix of the requested path and the remaining path segments are automatically appended to the target path. Here’s an example:
Redirect /foo/bar http://other.example/quux
If /foo/bar is requested, it’s getting redirected to http://other.example/quux; likewise /foo/bar/baz is getting redirected to http://other.example/quux/baz. Alias works alike but just internally.
In opposite to that, AliasMatch, RedirectMatch, and RewriteRule work with regular expressions and can be used not just to match path prefixes. Here’s an example:
RedirectMatch ^/foo/bar$ http://other.example/quux
If /foo/bar is requested, it’s getting redirected to http://other.example/quux (just like the Redirect example above). But if /foo/bar/quux is requested, it’s not getting redirected as ^/foo/bar$ doesn’t match /foo/bar/quux.
Futher differences between mod_alias and mod_rewrite are that mod_rewrite can also examine other parts of the URI except the path and even HTTP request header fields. It does also allow more complex operations with multiple conditions.

Related

301 redirects on WHMCS

I have installed WHMCS in a subdirectory, domain.com/whmcs
Now I am moving it into the main domain and I will be using custom pages. However, i don't want to have .PHP extension on the custom pages that I created. Ex. domain.com/page.php
I will need to remove .PHP extensions from specific pages and redirect these pages to non .PHP versions.
I just want to double check if this is the right 301 redirect for the search engines, before i publish it:
RewriteEngine on
RewriteRule ^mypage$ mypage.php [L]
Redirect 301 /mypage.php https: //domain.com/mypage
It works perfectly when i type domain.com/mypage.php redirects to domain.com/mypage But i am not sure if it's the right way to do it for the search engines. Currently i use wordpress in the root domain, so basically, I want to match the same links.
Thanks!
Just use this to redirect xyz.php to xyz:
Redirect 301 /xyz.php /xyz
As far as I know about search engines, its better to use a 302 redirect to avoid a 'permanent' issue.
Make sure that /xyz actually shows a valid page. So that you do not mess up with the search engines due to a simple mistake like that.
In your example, you use both mod_rewrite and mod_alias. One of them is enough.
RewriteCond, RewriteRule... are from mod_rewrite
Redirect, RedirectMatch... are from mod_alias
Always, test it once before working on a production server.

How to use .htaccess Rewrite to redirect all files with same name?

I have a bunch of files scattered around my server with the same name (let's call it abc.html). I want every single abc.html to be redirected to a single page (let's say /woo/xyz.html). Is there a way i can redirect all these pages to the one page without writing out each and every one? Thanks in advance!
The following rule should work, if I correctly understood your question:
RewriteRule ^.*/abc\.html$ /woo/xyz.html [L]
It will redirect any request to a file abc.html (even in subfolders) to /woo/xyz.html.
Unless you are already using mod_rewrite for other rewrites/redirects then it would be preferable to use a mod_alias RedirectMatch. For example, in the root .htaccess file:
RedirectMatch /abc\.html$ /woo/xyz.html
Matches any URL that ends /abc.html (which includes the document root and any subdirectory). The source URL-path is a regular expression.
This is a 302 (temporary) redirect.

301 redirect in .htaccess for 30,000 errors

I've been tasked to clean up 30,000 or so url errors left behind from an old website as the result of a redesign and development.
I normally use .htaccess to do this, but I doubt it would be wise to have 30,000 301 redirects inside the .htaccess file!
What methods have some of you used to solve this problem?
Thanks in advance.
Here as you can do with apache httpd
RewriteMap escape int:escape
RewriteMap lowercase int:tolower
RewriteMap my_redir_map txt:map_rewrite.txt
RewriteCond ${my_redir_map:${lowercase:${escape:%{HTTP_HOST}%{REQUEST_URI}}}} ^(.+)$
RewriteRule .* http://%1 [R=301,L]
I use this rewrite rules usually directly inside apache httpd configuration.
Inside the map_rewrite.txt file you have a tab delimited file with the list of redirect in the following format:
www.example.it/tag/nozze www.example.it/categoria/matrimonio
www.example.it/tag/pippo www.example.it/pluto
www.example.it/tag/ancora www.google.com
Would be much easier if you can generalize the approach because the redirect have a common pattern. But if not, in this case you only need to add the redirected url into the list.
Take care to study the RewriteMap configuration, because you can also write the list into a different format, for example like a database table.
Please pay attention to this: I have added escape and lowercase only because there are accents into the urls I need to write. If your urls doesn't have accents, you can remove both.
If you want implement these redirects in php, here the code you need:
<?php
$dest_url = "http://example.com/path...";
header("HTTP/1.1 301 Moved Permanently");
header("Location: ".$dest_url);
Create a PHP page to operate as a 404 handler. It should inspect the incoming URL, check if it should map from an old page to a new page, then issue a 301. If there is no mapping then present a 404.
Simply set this page as the 404 handler in your .htaccess and there you go. IIRC this is how Wordpress used to handle 'clean' URLs on IIS before IIS7 brought in URL rewriting without needing a 3rd-party dll.
I have made a redirect class that is on the 404 page that will check the database if there is a valid page to 301 redirect to and redirect it instead of giving the 404 page. If it can't figure that out, it marks it in the database as a 404 page, so it can be fixed later.
Thanks for help guys. I've carried out the suggested course of action from freedev but have created a separate config file within Apache.
Within the httpd.conf file I have added:
# Map settings
Include "conf/extra/map.conf"
The map.conf file:
RewriteEngine On
RewriteEngine on
RewriteMap url_rewrite_map txt:conf/map.map
RewriteCond ${url_rewrite_map:$1|NOT_FOUND} !NOT_FOUND
RewriteRule ^(.*) http://website.com/${url_rewrite_map:$1} [R=301]
The map.map file is formatted as:
/oldname/ /newname
I've added quite a few of the urls for the redirection and so far so good, it isn't having a massive impact on the server like it did when added to .htaccess

.HTACCESS - redirect www.example.com/side.php?id=* to www.example.com

I have recently assisted in moving a website from a pure development domain to a live site where there used to be a site handled by another CMS system than what we are currently using. Current system is Joomla, but I don't think it matters much for my question.
So with the current site the URLs are rewritten from the standard Joomla format to be stripped of index.php and .html suffix is added in the address, meaning that URLs look like this:
http://example.com/folder/page.html
In the old site handled by another CMS systems the URLs had the following structure:
http://example.com/side.php?id=1
We are a social organisation with many sites linking to us - also quite a few that we are not even aware of - so the problem I need to handle is this: I need to redirect all these dead links on other sites so that they simply get pointed to the root of our site.
Can anyone please explain to me how to make .htaccess redirect as follows:
/side.php?id=* to root of example.com
In this case I mean the * to mean any number as there are naturally alot of pages with different IDs.
It is not of any significance to me if they point at a www. prefix or not.
Thanks in advance for your help, I hope I have not asked a question that's been answered before but my experience with .htaccess is very limited and having searched and tried different solutions didn't do it for me.
In .htaccess in the root folder, add the following:
RewriteEngine on
RewriteCond %{QUERY_STRING} id=\d+
RewriteRule ^side\.php$ http://%{SERVER_NAME}/ [R=301,L,QSD] #Remove the ",QSD" for Apache <2.4.0, or to keep the query string.
The R=301 will tell browsers/search engines that the page has permanently been moved.
A rule like this should work:
RewriteCond %{HTTP_HOST} .*
RewriteCond %{QUERY_STRING} id=\d+
RewriteRule ^side.php http://example.com [L,R=301]
This will redirect externally, with a HTTP 301 response (Moved Permanently)
What you need here is some 301 Moved Permanently responses to let browsers and search engines know that you're moving pages to a new location. Instead of using a mod_rewrite to redirect all requests to this side.php page, I would analyze each page of your old website and determine where all the content has been moved. Armed with this, use the htaccess Rewrite directive to inform browsers of individual pages being moved
Redirect 301 /side.php?id=123 /about_us.html
Redirect 301 /side.php?id=456 /contact_us.html
I recommend this method because it redirects those who navigate to the outdated page to the new page that has similar content to the old one they were requesting instead of just redirecting them all to your home page.

Alias within htaccess, and block access to original file? (URL rewriting)

I have a very basic two-page website: the home page, and an about page. What I'm trying to do is use htaccess to rewrite the URLs so that the appear as:
domain.com/ (for the home page)
domain.com/about (for the about page)
In the actual folder structure of the site, the homepage is /index.php and the about page is /about.php (both appear in root).
I've been doing research into using alias but unfortunately my hosting (Dreamhost) doesn't allow access to httpd.config so that's out the window and I'm left with using rewrite rules in the htaccess file.
Since the index.php file will appear in the domain root (http://domain.com/) automatically, I've so far managed to make the about page appear correctly at domain.com/about using these lines:
RewriteEngine On
RewriteRule ^about$ /about.php
I'm also using 301 redirects so that for example, domain.com/about/ (with the trailing slash) also directs back to the /about URL like this:
Redirect 301 /about/ http://domain.com/about
This works great.
However the index.php and about.php files still also show if you go to the correct URL within your browser (eg: domain.com/about.php) so as a result the search engines are seeing (and indexing) two versions of each page! I've set up the correct canonical metadata within each page but this doesn't seem to have had any effect (the canonical metadata have been within the page markup ever since the site went live).
So how would I go about firstly doing a 'blind' rewrite (for want of a more technical term) for the two files so that the URLs look correct, but also effectivly 'block' direct access to the files - ensuring that if someone were to attempt to access the php files directly, the URL would still appear in the visitor's browser as the 'pretty' versions rather than the full file name and php extension?
Any advise would be hugely appreciated, I've been researching this for another couple of days now (I don't think there's anything quite the same as this anywhere on here already) and cannot for the life of me work this one out!
Check $_SERVER['REQUEST_URI'] in the PHP files. If that contains the filename (instead of the URI that you want), then do a 301 or 404 or whatever you want. Else serve the page as usual.
You can also do a
RewriteRule ^about.php - [L,gone]
or
RewriteRule ^about.php /about [L,R=301]
but this has to go before your other RewriteRules. It will send a 410 Gone or a 301 Moved Permanently response if the page is accessed via /about.php. See Apache Module mod_rewrite for the complete documentation of mod_rewrite.

Categories