301 Direct from old site folders to new site structure - php

Thanks in advance.
I have redone my website with new folder structures
Old site structure:
www.domain.com/folder/file.aspx
New site structure:
www.domain.us/folder/file.php
Notice the new TLD. I have around 20 folders that I want to redirect. Is it possible to do this with one file or do I have to do a 301 for each individual folder from the old site to the new site? What is the best way to handle this?
Again,
Thanks in advance

You should use mod_rewrite for that. Put this in the .htaccess file in the public_html folder of your FTP:
RewriteEngine On
RewriteRule ^([^.]+)\.aspx$ $1.php [L]
This should make sure that when someone is calling Banana.aspx they get to see Banana.php.

The .htaccess file in the directory takes precedence over any .htaccess file in the parent directories. So, if you already have .htaccess file in each of your sub-directories then putting an .htaccess file in your root directory makes no sense.
Remove the .htaccess files from each of your sub-directories that you want to redirect and update the .htaccess file in your root folder. You probably need to put 20 rewrite rules like this. Keep in mind that removing .htaccecss from sub-directories will remove all restrictions that you [might] have put on that folder.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com
RewriteRule ^folder/file\.aspx$ http://domain.us/folder/file.php [R=301,NC,L]
RewriteRule ^folder2/file\.aspx$ http://domain.us/folder2/file.php [R=301,NC,L]
301 for permanent redirect, NC for No Case, L for Last rule to process if matches
OR
Update the .htaccess file in each directory that you want to redirect
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com
RewriteRule ^(.*)\.aspx$ http://domain.us/folder/$1.php [R=301,NC,L]
#the following might also do; just test it out
#RedirectPermanent (.*)\.aspx$ http://domain.us/folder/$1.php
So,, depending on your situation, you pick one. I'd go with option one so that all redirects are in one place. You can always move the restrictions from subdirectories' .htaccess to root's .htaccess.

If you use 301 PHP header() redirect, then I'm afraid it can only be done one-by-one.
But if you use mod_rewrite to handle this for you, then this is a relevant source for you to have a look: http://www.gnc-web-creations.com/301-redirect.htm

Related

How do I get mod-rewrite to access the proper directory?

I'm trying to use mod-rewrite to eliminate urls like {root}/index.php?action=about&page=faq. I want {root}/about/faq to redirect to the php url. My .htaccess file looks like
DirectoryIndex index.php
RewriteEngine On
RewriteRule ^(index|about|calendar|news|contact)$ index.php?action=$1 [L]
RewriteRule ^(index|about)/(.+)$ index.php?action=$1&page=$2 [L]
When redirecting to index.php?action=$1, this works fine. However, it doesn't work completely when the second rule is used. The html content appears fine, but the stylesheet and images are missing. The page seems to think that it's in a subdirectory. So {root}/about/faq looks for images in the folder about/images, instead of in the images folder found at the root. The PHP script says that the working directory is still the root directory, however. How do I make the pages think that they are in the root directory? Thanks!
RewriteRule ^(index|about|calendar|news|contact)/(.*)/$ /index.php?action=$1&page=$2
Would result in /about/faq/ rewriting to index.php?action=about&page=faq
/calendar/month/ would result in index.php?action=calendar&page=month
Also unless you want to rewrite imagesfiles I would add:
RewriteCond %{REQUEST_URI} !(\.gif|\.jpg|\.png)$ [NC]
This will stop image requests being rewritten.

how to modify this mod rewrite into the url I want?

I actually got an answer that if I want to change my url from www.mydomain.com/laravel4/public/posts into www.mydomain.com/posts
this is what I have to do with my .htaccess
RewriteEngine On
RewriteRule !^laravel4/public/ /laravel4/public%{REQUEST_URI} [L,NC]
but I have another question if I want the url to be www.mydomain.com/laravel4/posts which takes out the public what should I modify? I thought taking out the laravel4 in the code above would work but doesn't seem that easy :(
Ideally you should move the Laravel install out of the document root and just move the contents of the public folder into your document root and edit the index.php and bootstrap/paths.php accordingly, or alternatively set the document root to the public folder of Laravel, some hosts will change the document root for you if you open a support ticket.
However thats not always possible depending on your host, and most people will tell you to go off and find a better host, but again, thats not always an option.
Below is a .htaccess example you can use to rewrite all URLs to the public/index.php of Laravel, but be warned, this isn't a very secure way to setup Laravel, and anyone could potentially access any of your configuration and other files that exist in your document root, exposing your database passwords and secret keys used to encryption.
RewriteEngine On
RewriteRule ^ laravel4/public/index.php [L]
Updated as per request
RewriteEngineOn
RewriteRule ^laravel4/.* laravel4/public/index.php [L]
You can use this rule:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/laravel4/public(/|$) [NC]
RewriteRule ^laravel4/(.*)$ /laravel4/public/$1 [L,NC]

.htaccess internal redirect not working for subfolder

I have a site that has another site inside of it in a sub folder. For example, www.domain.com and the sub folder would be www.domain.com/uk/. They both contain duplicate files, so even the .htaccess file is same. Now the problem is when I redirect users through .htaccess inside the uk subfolder to a file in the same directory it redirects them to the file in the parent site with the duplicate name.
This is the rule I have used in my .htaccess file:
RewriteRule ^category/[a-zA-Z0-9_-]+/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ final_final.php
I don't have much experience with mod_rewrite.
in the parent directory RewriteBase should be /
RewriteEngine On
RewriteBase /
in the subdirectory the RewriteBase should be /uk
RewriteEngine On
RewriteBase /uk
you can also specify concrete path in concrete redirect, it is not quite "clean" like Gustonez way but it may work too
in uk-htaccess redirect to uk base directory
RewriteRule ^category/(.*)$ /uk/$1 [L,R]
and in parent-htaccess redirect to base directory without /uk/
RewriteRule ^category/(.*)$ /$1 [L,R]
but redirect you have is weird i think
^category/[a-zA-Z0-9_-]+/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ final_final.php
do you really need all this parameters in url to redirect?
and when you redirect any of these parameters will be parsed on page
you just going to final_final.php page without sending parameters to this page
another thing is that you don't have [L] flag a the end of statement
you better tell exactly what you need because this redirect you have may be inadequate and may not work at all

Redirecting all request to the root directory somewhere else?

Alright, so I've downloaded a CMS for a server I'm setting up and I have some difficulty with the path the files are on.
I have put the CMS in a subdirectory on my server as the root directory is already being used by another CMS. However, the CMS repeatedly uses "/" in the code to link to their files. For example, an image is found using this code:
<img src="/images/banner.png" />
As you know this will not work, because the link above redirects the request to the images folder in the root of the server, not the subdirectory. The CMS also came with a ".htaccess" file, so I immediately thought that I could redirect all requests made using the "/" character to the subdirectory on the server. This is the original ".htaccess" file:
RewriteEngine On
RewriteRule ^index.php(|/)$ content/.php
RewriteRule ^error.php(|/)$ content/error.php
RewriteRule ^housekeeping(|/)$ housekeeping/index.php
RewriteRule ^(|/)$ content/$1.php
RewriteRule ^([a-zA-Z0-9_-]+)(|/)$ content/$1.php
RewriteRule ^rd/([^/]+)(|/)$ /rd.php?id=$1
RewriteRule ^quickregister/start(|/)$ /register/start.php
RewriteRule ^quickregister/step2(|/)$ /register/step2.php
RewriteRule ^quickregister/step3(|/)$ /register/complete.php
RewriteRule ^community/online(|/)$ content/online.php
RewriteRule ^community/vip(|/)$ content/vip.php
RewriteRule ^credits/goldvip(|/)$ content/goldvip.php
RewriteRule ^home/(..*)$ content/home.php?u=$1
RewriteRule ^articles/(..*)$ content/articles.php?story=$1
ErrorDocument 403 /content/error.php
ErrorDocument 404 /content/error.php
I thought that by adding
RewriteRule ^/$ /subdirectory/
the requests to "/" would be redirected, but to no avail. I have checked the apache configuration and it seems that overwriting the config using htaccess files is enabled, so if anyone is able to help me out I'd be very happy.
Thanks in advance!
EDIT:
I came real close to a solution. I inserted this code just below "RewriteEngine on":
RewriteRule ^(.*)/(.*)$ /private_servers/strebbohotel/$2
This returned the following error page when visiting the url "http://mysite.com/private_servers/strebbohotel/content/.php":
Not Found
The requested URL /private_servers/strebbohotel/.php was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
As you can see, it skips the "content" subdirectory for some reason. I believe it has something to do with the other lines of the .htaccess file but I can't figure out which ones. It also could be that I need a more complex regex, but I'm not particularly good with it so if someone could help me further I'd be thankful.
You have to change the .htaccess file on yout root directory - and that will mess with the other CMS. The only solution that comes to my mind is to use combination of RewriteCond's:
RewriteCond %{HTTP_REFERER} my_better_cms_url_regex
RewriteCond %{REQUEST_URI} ^/images/.*$
RewriteRule ^.*$ /subdirectory/%{REQUEST_URI}
That should do the trick. If you want to customize it, refer to this cheatsheet (or simmilar). But be aware, that .htaccess in root directory is checked against ALL requests made to your subdir cms - therefore the need for second condition.
The .htaccess file in your CMS subdirectory would have no effect on requests made against the server root directory. You would need to add an .htaccess there.
Even then though, I don't know how you would differentiate requests from the CMS (like the image request in your example) from those intended for the application in the web root.
Does your CMS not have any configuration setting that allow you to specify a path the the files other than the web root?
You want to use:
RewriteBase /another_root_dir/
in the beginning of your .htaccess file

htaccess mod_rewrite help

i have a domain with a website in a folder.
i.e
http://domain.com.au/website/page.php
i only have access to .htaccess files to make the rules.
i would like my url to look like this..
http://domain.com.au/page
so in essence i want to drop the subfolder its sitting in, and the php extention.
All links in my php are structured like this though
page link
this is the same for js and css. they are all referenced from the 'website' folder.
will the rewrite mean i have to change all my links?
priority is dropping the folder from the url, not so much the php extension.
1) You could move all the files to the root web directory and run the website from there.
2) You would have to manually modify every link to remove the website directory from the beginning and then use the .htaccess file to redirect the pages back to the appropriate page. If you're going to do it this way, you might as well remove the PHP extension...
RewriteEngine On
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^domain.com.au$ [NC]
RewriteRule ^(.*)/ website/$1.php [L]
You may have to modify the website/$1.php part to include your entire root directory in some cases, but that should work as long as the .htaccess file is in the root web directory. That will redirect any page in the form 'http://domain.com.au/page' to 'http://domain.com.au/website/page.php' while keeping the URL the same in the address bar.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^page\_one/(.*)/([0-9]{1,6})/?$ webroot/products/products/view/$2 [L]
</IfModule>
this htaccess code redirects all trafic from www.domain.com/page_one/anything/numeral
to the page www.domain.com/webroot/products/products/view/same numeral
for your example, something like
RewriteRule ^page?$ website/page.php$2 [L]
note, this works for "page" as a string, if you need other functions , change it
for more insight, you might want to take a look at this link :
http://www.javascriptkit.com/howto/htaccess.shtml

Categories