Removing .html and .php from links in IIS - url

As the title says, I would like to know how to remove ".html" and ".php" from links in a Windows server (IIS).
Most of the information I have found about this subject was only valid for Linux servers.
Only today I heard about the file web.config and got new material to study. Nevertheless, I am still struggling with it.

You can use the url rewrite module for IIS: http://www.iis.net/learn/extensions/url-rewrite-module/using-the-url-rewrite-module
Here is a good tutorial on the subject: http://www.surfingsuccess.com/asp/iis-url-rewrite.html

Read about friendly-url and .htaccess
See an example:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([a-z0-9-]+)/?$ index.php?pag=$1 [NC]
RewriteRule ^([a-z0-9-]+)/([0-9]+)/?$ index.php?pag=$1&id=$2 [NC]
</IfModule>
That way when you use:
localhost/contact
The server will do an internal redirect to:
localhost/index.php?pag=contact

Related

Zend Framework 2 without Vhost configuration

I had finished my first web application using Zend Framework 2 and I'm about to put it online.
But may web host doesn't allow me to change my vhost configuration!
The .htaccess file is allowed.
So my question is: How to set up my ZF2 app with only .htaccess files?
Assuming you have a standard ZF2 application based of the skeleton, then try creating a .htaccess file in the root with this in it:
RewriteEngine On
RewriteRule ^\.htaccess$ - [F]
RewriteCond %{REQUEST_URI} =""
RewriteRule ^.*$ /public/index.php [NC,L]
RewriteCond %{REQUEST_URI} !^/public/.*$
RewriteRule ^(.*)$ /public/$1
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^.*$ - [NC,L]
RewriteRule ^public/.*$ /public/index.php [NC,L]
I haven't tested this, but in theory, this should effectively move the document root to the public directory while keeping it out of the url.

redirect a url or domain with www

I have a simple question about url or domain redirect. I was looking through the previous questions on how to redirect a url or domain from http to http to force using ssl.
The weird part shows up when I try to redirect none www. to www. with the domain. I tried many ways, but none worked for me. does any one have any idea why is that?? and how to resolve this issue??
here is the code for I used at .htaccess:
RewriteCond %{HTTP_HOST} ^mydomain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.mydomain\.com$
RewriteRule ^/?$ "https\:\/\/www\.mydomain\.com\/main" [R=301,L]
another weird thing is that whenever I typed the domain on the browser without www. there is an error message, but when I insert the www. in front of the domain, it redirects with https and wwww of course.
any idea?
Thanks
You rules will run into a loop. Do this instead:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

.htaccess rewrite frustrations

I made my website in PHP originally and used parameters for page/product identification.
ex.) http://mysite.com/?pid=my_products&sid=golf_clubs&GTIN=111111111111
I've now moved over to using RoR and I'd like to set up redirects to clean up the urls
http://mysite.com/my_products/golf_clubs/111111111111
I've looked at tutorials and arrived at this:
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ ?pid=$1&sid=$2&GTIN=$3 [NC]
I've tested a url and it doesn't redirect at all. I've tried putting this in the .htaccess file to debug and chmodded the log folder to 775 but I get a 500 error:
RewriteLog "/home/myaccount/rails_apps/myapp/log/rewrite.log"
RewriteLogLevel 3
As far as rewrite statements in .htaccess goes, this is what I have:
RewriteEngine On
RewriteLog "/home/myaccount/rails_apps/myapp/log/rewrite.log"
RewriteLogLevel 3
RewriteBase /
RewriteCond %{REQUEST_URI} ^/downloads.*
RewriteRule .* - [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ pid=$1&sid=$2&GTIN=$3 [NC]
RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
ErrorDocument 500 "<h2>Application error</h2>Rails application failed to start properly"
* I am on a shared hosting plan with Bluehost (good gawd I wish they'd update to Rails 3 soon)
If I understand you correctly, you have your rule backwards. You want to catch your old URLs and rewrite them to the new format, correct? In that case, you want this:
RewriteRule ^\?pid=(\w+)\&sid=(\w+)\&GTIN=(\w+) /$1/$2/$3 [NC,R=301,L]
Adding a 301 status will tell the user agent (including well-behaved search bots) that your canonical URLs have permanently changed.

Symfony 1.4 Error 400 on the home page in prod env (not in dev env)

I have made an tested an application on my computer and all is working fine (I used only the dev environment via the frontend_dev.php page).
When I deploy this application on the test server, I have the error 400 Bad Request below:
Bad Request
Your browser sent a request that this server could not understand.
I accessed to the website with the normal URL.
I'm using symfony 1.4 with doctrine (no others plugins are involved), wamp 2 (php 5.3, apache 2.0.59).
If I try to access the application with the dev environment, it works.
Try checking if the no_script property on your application setting.yml file. If should be set to False, clear cache and try again
Thank you very much Guiman. You have leaded me to the answer.
The no_script_name property wasn't the origin of the problem. Instead, I have my .htaccess in the web directory which was badly generated (I swear on my pet's head that I haven't edited this file before). Below is the file generated by symfony (the bold attribute isn't working within the code one, look at the last line):
Options +FollowSymLinks +ExecCGI
<IfModule mod_rewrite.c>
RewriteEngine On
# uncomment the following line, if you are having trouble
# getting no_script_name to work
#RewriteBase /
# we skip all files with .something
#RewriteCond %{REQUEST_URI} \..+$
#RewriteCond %{REQUEST_URI} !\.html$
#RewriteRule .* - [L]
# we check if the .html version is here (caching)
RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
# no, so we redirect to our front web controller
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
And this is the updated one (again, look at the last line):
Options +FollowSymLinks +ExecCGI
<IfModule mod_rewrite.c>
RewriteEngine On
# uncomment the following line, if you are having trouble
# getting no_script_name to work
#RewriteBase /
# we skip all files with .something
#RewriteCond %{REQUEST_URI} \..+$
#RewriteCond %{REQUEST_URI} !\.html$
#RewriteRule .* - [L]
# we check if the .html version is here (caching)
RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
# no, so we redirect to our front web controller
RewriteRule ^(.*)$ **/myApp/index.php** [QSA,L]
</IfModule>
I don't know what happened but I will check on the symfony website I there are similar issues.

Redirect "mydomain.com/user" to "mydomain.com/name.php?id=user"

I think I can use the .htaccess file for this, but I've looked it up and not found anything useful. What I want to do is have my site redirect to a php page when a user types their username in the URL like:
example.com/username
And have it be the same as a PHP page like:
example.com/name.php?id=username
I'd like it to display as example.com/username even after it redirects, but it is not necessary. Any ideas?
You can use mod_rewrite to transparently rewrite your URLs on the server.
Assuming that you'd only have usernames following your domain, something like this would do what you want:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .+ name.php?id=$0
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.+) users.php?user=$1 [L]
I think that will work.
The Apache mod_rewrite guide is here
http://httpd.apache.org/docs/2.0/misc/rewriteguide.html
You probably want Apache's mod_rewrite.

Resources