At first I used IIS server for PHP, so I'm new to Apache (Windows platform). I am in some puzzle related with URL rewrite. As in my .htaccess file I use the below code for URL Rewrite.
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)\.aspx$ $1.php [nc]
Now If I browse a .php page with .aspx extension, it works well. eg If I browse
www.example.com/guwahati.php => www.example.com/guwahati.aspx (both URL works.)
But I want it automatically. Like if I type www.example.com/guwahati.php, it will automatically convert to www.example.com/guwahati.aspx, is it possible ? also URL rewritting is not working on my localhost. may anyone help me please.
But I want it automatically. Like if I type www.example.com/guwahati.php, it will automatically cconvert to www.example.com/guwahati.aspx, is it possible ?
It is possible, but by using redirect headers, not URL rewrites. Actually you could do an URL rewrite that redirects to a single file that replaces the target extension with aspx and then redirects to it. You may need to enable mod_rewrite in order for it to work.
Related
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
I'm in a situation where my web page have the following types of URL's
http://mysite.com/?type=home
http://mysite.com/?type=aboutus
http://mysite.com/?type=contactus
For the end users I need it to display like the following:
http://mysite.com/home
http://mysite.com/aboutus
http://mysite.com/contactus
This means, the user is not aware that the URL have a request parameter "type" in it.
I'm not sure it is possible or not.
Please help me to get to a solution.
EDIT: I searched lots of websites to learn URL rewrite by using .htaccess, But didnt able to make them working in my server.
Place this in you .htaccess file
RewriteEngine On
RewriteRule ^([a-zA-Z0-9-+/]+)$ ?type=$1
RewriteRule ^([a-zA-Z0-9-+/]+)/$ ?type=$1
Yes it is absolutly possible.
Your have to be sure your hostprovider has enable URL rewriting (I don't really remenber but I'm sur you can find help on Internet to verify that).
You have to create an ".htaccess" file at the root of your site.
Put this content in it :
tell the server to follow symbolic links :
Options +FollowSymlinks
activate the rewrite module :
RewriteEngine on
Rule for rewrite url
RewriteRule ^([a-z]+)$ /index.php?type=$1 [L]
Note this is just a qucik exemple you may want to look "url rewriting tutorial" on Google.
I have a website that passes some GET variables to different pages in PHP. My issue is that now I have a url with variables i.e. index.php?category=categoryname and that's not very memorable.
Is there any way I can change the URL to something like /categoryname instead without duplicating the page and storing in folders? But also allow users to type in /categoryname and be redirected to the correct page?
.htaccess Apache mod_rewrite, almost every professional dynamic website uses this method (like stackoverflow).
The method is fully explained in this article far better then I could ever explain it in this answer box.
You should look into writing some apache Mod_Rewrite rules in a .htaccess file.
The solution is discussed here:
this is done by the rewrite module of apache and this handles regular
expressions. You have to put a rule
like this in your .htaccess file on
the root of your website:
RewriteRule ^cat/([0-9]+)$
/index.php?category=$1
^ means the start of the url after
www.example.com/ $ means the end of
the page.
www.example.com/cat/123
will be converted by the server to:
www.example.com/index.php?category=123
In PHP you use the normal $_GET['id']
variable. The rewrite module must be
enabled by apache... This is mostly
used to make the url format
independent of the serverside
scripting language so the .php in the
url is not logical. Thats why i
changed it to product/ . The .htaccess
starts with
RewriteEngine On Options
+FollowSymLinks RewriteBase / Here all the rewrite rules.. ...
I want to hide file extensions from a URL like
if the current URL is
http://localhost/salsgiver/administrator/menus.php?sect=about
then the new one will be exactly
http://localhost/salsgiver/administrator/menus/sect/about
and so on, similary if the current URL is
http://localhost/salsgiver/administrator/products.php?id=1
then the new one will be exactly
http://localhost/salsgiver/administrator/products/1
Or some thing different, so that the viewer could not guess the exact URL.
I searched Google and found some matter on
http://roshanbh.com.np/2008/02/hide-php-url-rewriting-htaccess.html
and also used, but it does not work and the mod_rewrite module is also enabled in Apache. And when I create the .htaccess file to secure a folder from all using
deny from all
it works fine.
You could do this, but mod_rewrite is much easier to use if you use a single index.php which then chooses which file to open (products.php or menus.php)
For a single index file:
RewriteRule ^(.*)$ index.php?query=$1 [L]
For multiple files:
RewriteRule ^(.*?)/(.*?)/(.*?)$ $1.php?$2=$3 [L]
Is it possible to redirect a user to a php page and then redirect to different image, if the user is requesting for the image ?
For example if user requests for the image or if other website requests for the image, it should be redirected to the php page and then redirected to a different image.
Like if other website requests for http://example.com/images/a.gif, the website will get a different image i.e. http://example.com/images/b.gif.
Is it possible? Let me know if I am not clear with my problem.
Thanks.
Looks like a use case for mod rewrite
with something like:
RewriteRule /images/(.*).gif$ images.php?img=$1
Do you mean you want to redirect to a different image, if the request comes from outside your site? Then you need apache's mod_rewrite, and rules something like this:
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http://your\.site\.com/ [NC]
RewriteRule a.gif b.gif [L,R]
It means: if the referer is not a part of your site, rewrite every request to a.gif to b.gif.
The RewriteRule specified above would be best, if you're using Apache. You could do it either via the server's config files or through a .htaccess rule if your server supports it. For more info, look to Apache's modrewrite page (for version 2.2): http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html
If you're using IIS or another web server, they all allow similar redirects, they just have different ways of implementing them.