HTML apache rewrite to new directory - html

I have been working on this for over an hour, and clearly I'm missing something.
I need to redirect:
www.site.com/directory/file.php?m=12345 to www.site.com/file2.php?m=12345
No matter what I am doing, the rule is not met, the data is missed or the redirect tries to go to the same directory.
I am doing the redirect in .htaccess.
The RewriteEngine is on and other redirects are working.
There is not a stop before the rewrite to prevent the rule from being read.
I am using these flags NC, R, L
DirectoryIndex Default3.php Default3.PHP
#Block listing of folder contents
IndexIgnore *
RewriteEngine on
#Make access to image folder case insentive
RewriteRule ^images/(.*) Images/$1
RewriteRule ^common/(.*) Common/$1
#redirect old photo download page
RewriteRule GetZipPhotos.*(\d*) PhotoDownload.php?mls=$1 [NC,QAS,L]
Thanks for any help.
I have changed the .htaccess to
DirectoryIndex Default3.php Default3.PHP
#Block listing of folder contents
IndexIgnore *
RewriteEngine on
#redirect old photo download page
RewriteRule ^Agents_Admin/GetZipPhotos.php(.*) /PhotoDownload.php$1 [R,NC,L]
#Make access to image folder case insentive
RewriteRule ^images/(.*) Images/$1
RewriteRule ^common/(.*) Common/$1`
The rewrite is still not working. According to the htaccess tester this does catch the original URL and should redirect correctly. But it is not. The original file keeps getting ran.

RewriteRule ^/Agents\_Admin/GetZipPhotos.php$ /PhotoDownload.php [R,NC,L,QSA]
QSA means Query String Append, in the (.*) and $1 are only URI parts, not parameters

Related

Apache rewrite exclude empty URI

The following rewrite appends .html to the URI.
RewriteRule ^/(.*)$ /sites/uk/$1.html [NC,L]
However, when it comes to the index page it is giving me:
.../sites/uk/.html instead of:
.../sites/uk/index.html
What I want to know is how to exclude an empty URI so it doesn't append .html to nothing. I have tried the following rewrite condition:
RewriteCond %{REQUEST_URI} !^/?$ However this is unsuccessful.

Use htaccess to fix misspelled urls

So I have a pretty simple problem (at least I think do) with my website. I need to be able to redirect any misspelled URLs to the correct ones. It's easier if I explain it to you guys than to describe it.
For example, let's take this url.
http://www.tomshardware.com/reviews/radeon-r9-290x-hawaii-review,3650.html
Now, that url will take you to the correct page of that article regardless of how the url is spelled. Say you accidentally place a letter, number or a word into that URL to something like this:
http://www.tomshardware.com/reviews/radeon-r9-290x-TEST-TEST-hawaii-review,3650.html
That url will still take you to the correct article and fix itself to the correct URL. You could add anything to that URL and it will still take you to the right article regardless what you accidentally type into it.
So my question is how do I do this in htaccess? This is my current htaccess file
# Secure htaccess file
<files .htaccess>
order allow,deny
deny from all
</files>
AddHandler application/x-httpd-php5 .html .htm
AddType application/x-httpd-php .html .htm .php
AddHandler cgi-script .pl .cgi
Options ALL -Indexes -Multiviews +ExecCGI +FollowSymLinks
# Do not remove this line, otherwise mod_rewrite rules will stop working
RewriteBase /
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
#Redirect Non-WWW to WWW
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_URI} /index\.html?$ [NC]
RewriteRule ^(.*)index\.html?$ "/$1" [NC,R=301,NE,L]
You probably can't do that in that way.
As you can observe, the text on the url is totally irrelevant and is only there to create readable and index-friendly (SEO) urls. Those words are called "slugs", see http://en.wikipedia.org/wiki/Clean_URL#Slug
If you modify the last part, the 3650 it will break the url because this is the only identifier which typically corresponds to a unique ID in the database.
Assumption on how and why the mentioned site do this:
The site uses either a standalone routing component (e.g. Routing from Symfony PHP framework: http://symfony.com/components/Routing), an entire web framework or everything is written by hand. Depending on the language it might be ZEND, Symfony, etc for PHP, MVC for Asp.net or any other.
In all cases there is some sort of filtering of urls before the original content is served.
The routing parses the url, retrieves the unique ID, fetches the data set and creates again an absolute URL out of it.
It then compares the freshly generated route with the one you have entered.
If they don't match the framework issues a http status of 30x and redirects you to the new url.
The purpose of that is to maintain link sanity when the slug tags have changed or for whatever reason the SEO friendly url layout have changed.
The redirect is there so the old fashioned urls are updated next time a search engine visits the page and updates it's index.
Imagine you have a typo somewhere in the slugs or you forgot to mention Radeon and you want to avoid having it forever broken or wrong in the DB.
So you need to fix it but at the same time you want to avoid breaking the old urls for search indexes which have not yet revisited your site with the new slugs or users that have bookmarked it.
After the redirect it again compares the urls and after they match the content is served.
A DB lookup is very likely here and you cannot do this properly with htaccess alone as you have no knowledge about correctness of the url here.
You would internal-redirect all article pages to a php program and it will match the parameters with best possible page to show
-- .htaccess --
RewriteEngine on
RewriteRule ^article/(.*).html$ /article.php?url=$1 [L]
-- php --
read article selection criteria
$article_url=$_GET['url'];
Search through database or files and show the article

URL rewrite in html

I want to do URL rewrite in html pages.
Any help for that. every one knows about url rewrite but I found all article for pp, asp.net, classic asp. so please any one knows how to do url rewrite in html.
like wise I want to rewrite from
http://www.xyz.com/aboutus.html to http://www.xyz.com/About-us
Any help will appreciated.
Thank you.
I'm not normally huge on working with RewriteEngine, or .htacess, but according to this blog entry, you can use the following code to hide file extensions:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
Just paste that in your .htaccess file, and put the changes to the server (upload the new .htaccess using an HTTP client). If your .htaccess already has RewriteEngine on, you should skip the first line.
If you want to change the URL from xyz.com/aboutus.html to xyz.com/About-Us, you also have to change the name of the file or folder aboutus to About-Us. Another possible solution would involve just having an index.html file in a folder named About-Us, which would make the server load that file automatically once a user accesses xyz.com/About-Us, and wouldn't display the filename.
To rewrite urls you need to configure the webserver to do that (for example in apache with .htaccess.
But if you want to do that without use server configuration, a bad solution exists: make a folder with the name of the url and put the html into that with the name index.php. For example about-us.html > about-us/index.html and in links put the url about-us. But is a bad solution.

CodeIgniter URL Rewrite

I have found so many examples of URL Rewrite in CI, but not a final and good solution, i have url like this
www.somesite.com/index.php/tehnicki/tekstovi
I want to have url like this
www.somesite.com/about.html
This about is GET Variable, that is going in MySql Query, it will be no number it will be some NAME, how to do that?
And what i have to write in tehnicki controller to take that GET Variable, is there any helper in CI for MySQL Attack, or i have to write it on my own :)
Okay, let's see, it seems messed up and it doesn't make sense but I'll give you what you need.
First we will have to remove the index.php file :
- use this as .htaccess
//.htaccess
RewriteEngine on
RewriteCond $1 !^(index\.php|Skin|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
next, in your config.php set index_page to an empty string
// application/config/config.php
$config['index_page'] = '';
on the same file, scroll to url_suffix, and set it to .html
// application/config/config.php
$config['url_suffix'] = '.html';
now head to your routes.php and apply a rewrite rule
// application/config/routes.php
$route['about'] = "tehnicki/tekstovi";
I'll quickly explain what I did here so you get how it works:
1- we removed the index.php section of the url in order to clean it
2- we added a url suffix that will be generated on all the pages
3- we applied a rewrite rule that will match /about.html to the controller tehnicki and the action tekstovi (tehnicki/tekskovi)

How to avoid double google indexing using .htaccess?

I have a website, with a nice RewriteRule in its root, that redirects all the queries of this kind:
http://domain.com/foo/parameter
into
http://domain.com/index.php?args=parameter
Users can only see the clean URL and everyone is happy.
Now here is the problem: domain.com DNS have an A record for domain.com, pointing to a private server IP, and an A record for mail.domain.com, pointing to the exact same IP.
For some unknown reason, in the last couple of months, Google double indexed all the pages of my site (http://domain.com/foo/par1, http://domain.com/foo/par2 etc.) with another set with the mail subdomain (http://mail.domain.com/foo/par1, http://mail.domain.com/foo/par2 etc).
I thought I could get rid of all of them redirecting any request to mail.domain.com/$whatever to domain.com and eventually Google would understand that all those pages with the 'mail' subdomain redirects to the homepage and are therefore not necessary.
I tried this in .htaccess:
RewriteCond %{HTTP_HOST} ^mail.domain.com$ [NC]
RewriteRule ^(.*)$ http://domain.com [R=301,L]
But this redirects to a visible URL that looks like this: http://domain.com/index.php?args=parameter, while I just want a redirect to the homepage.
What's the correct form, and are there more elegant ways to achieve this, maybe adding something into robots.txt? (Please note that I can't just disallow a subfolder here)
If you just want to redirect to home page by discarding the original REQUEST_URI and QUERY_STRING then use these rules:
RewriteCond %{HTTP_HOST} ^mail.domain.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/? [R=301,L]
By putting ? in the end it will strip out original query string, thus a URL of this type: http://mail.domain.com/index.php?args=parameter will become http://domain.com/
Your rule is correct, but you need to put it before all the other rules (right after RewriteEngine On) or it will pick up the latest state of the internal rewritten URL.
Update: Hmm, you said that your old rule redirects correctly but is using the internal, ugly, URL. That actually shouldn't be the case unless you add $1 to pick out the matched string.
RewriteCond %{HTTP_HOST} ^mail.domain.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]