Ylama
Ylama

Reputation: 2489

customize url using Rewrite rule in .htaccess

Basic question I am struggling to find an solution for.

I have an url www.example.com/signup/form.html i would like to rewrite the url as www.example.com/signup removing the form.html from the url but stil direct to this html file.

I have tried some sulotion to this, i do have a .htaccess file in my root with RewriteEngine on.

for some reason for a second it loads as www.example.com/signup and the suddenly redirects to 404 - page not found.

This is what i currently use (not working):

RewriteRule ^signup/form.html.*$ http://www.example.com/signup/ [R=301,L]

Also tried this :

RewriteRule ^signup/(.*)$ http://www.example.com/signup [R=301,L]

Also i have this on the top of my .htaccess file, might this be the reason i get the 404 ?

<Files ~ "^.*\.([Hh][Tt][Aa])">
order allow,deny
deny from all
satisfy all
</Files>
<Files ~ "^.ht">
Order allow,deny
Deny from all
</Files>
<Files .htaccess>
order allow,deny
deny from all
</Files> 
<Files ~ "includes$">
Order allow,deny
Deny from all
</Files>
<Files ~ "mods$">
Order Deny,Allow
Deny from all
</Files>
php_flag session.use_trans_sid off
php_flag error_reporting off
Options -Indexes
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
FileETag none
ExpiresActive On
ExpiresDefault A0
ExpiresByType image/gif A2592000
ExpiresByType image/png A2592000
ExpiresByType image/jpg A2592000
ExpiresByType image/jpeg A2592000
ExpiresByType image/ico A2592000
ExpiresByType text/css A2592000
ExpiresByType text/javascript A2592000

<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
Header set Expires "Thu, 15 Jan 2035 20:00:00 GMT"
</FilesMatch>

ErrorDocument 404 /notfound

DirectoryIndex index.php
RewriteEngine on 
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]



Redirect 301 /default.aspx http://www.example.com
Redirect 301 /home.asp http://www.example.com

Redirect 301 /contact.aspx http://www.example.com/contact/enquire
Redirect 301 /awards.aspx http://www.example.com/awards
Redirect 301 /event.asp http://www.example.com/visitus/events

Redirect 301 /social.aspx http://www.example.com/visitus/events
Redirect 301 /recipes.aspx‎ http://www.example.com/news/09/recipes
Redirect 301 recipes.aspx‎ http://www.example.com/news/09/recipes
Redirect 301 /recipes.asp http://www.example.com/news/09/recipes
Redirect 301 recipes.asp http://www.example.com/news/09/recipes
Redirect 301 /styles http://www.example.com/


Redirect 301 /home.asp?pid=24&page=4 http://www.example.com
Redirect 301 /home.asp?pid=13&page=2 http://www.example.com
Redirect 301 /home.asp?pid=42&page=3 http://www.example.com
Redirect 301 /home.asp?pid=12&page=1 http://www.example.com
Redirect 301 /home.asp?pid=71&page=3 http://www.example.com
Redirect 301 /event.asp?pid=30&page=2 http://www.example.com/visitus/events
Redirect 301 /event.asp?pid=63 http://www.example.com/visitus/events


RewriteRule ^pg([0-9]+)$ index.php?content=page&id=$1
RewriteRule ^([a-z]+)-pg([0-9]+)$ index.php?content=$1&pg=$2
RewriteRule ^([a-z]+)-([0-9]+)$ index.php?content=$1&id=$2




RewriteRule ^([a-z]+)/0([1-9]|[1-9]([0-9]+))/pg([0-9]+)/([a-z0-9\-\/]+)$ index.php?content=$1&cat=$2&pg=$4

RewriteRule ^([a-z]+)/0([1-9]|[1-9]([0-9]+))/([a-z0-9\-\/]+)$ index.php?content=$1&cat=$2
RewriteRule ^sitemap.xml sitemap.php

RewriteRule ^([a-z]+)-day-([0-9]{4})-([0-9]{2})-([0-9]{2})$ index.php?content=$1&view=day&y=$2&m=$3&d=$4
RewriteRule ^([a-z]+)-month-([0-9]{4})-([0-9]{2})$ index.php?content=$1&view=month&y=$2&m=$3
RewriteRule ^([a-z]+)-year-([0-9]{4})$ index.php?content=$1&view=year&y=$2

RewriteRule ^([a-z]+)-archs([0-9]+)$ index.php?content=$1&view=archives&pg=$2

RewriteRule ^images/lightbox-btn-next.gif images/lightbox-btn-next.png
RewriteRule ^images/lightbox-btn-prev.gif images/lightbox-btn-prev.png
RewriteRule ^images/lightbox-btn-close.gif images/lightbox-btn-close.png

DirectorySlash On
DirectoryIndex form.html

I did research here on this , but i dont know im struggling to get the syntax correct.

Upvotes: 1

Views: 1050

Answers (3)

Ylama
Ylama

Reputation: 2489

I fixed the issue in a way a not really wanted to but anyways,my solution is.. added my .html file to the root(not in any directory) and renamed the file to signup.html and the used .htaccess Rewrite to remove the .html

.htaccess (Rewrite)

 DirectoryIndex index.php
 RewriteEngine on
 RewriteCond %{REQUEST_FILENAME}.html -f
 RewriteRule !.*\.html$ %{REQUEST_FILENAME}.html [L]

Also added an redirect for any person that already have the previous address:

.htaccess (Redirect)

Redirect 301 /signup/index.html http://www.exapmle.com/signup

Did not really want to move my .html file from sub directory but this was my only solution that 'worked'.

Upvotes: 1

Joe
Joe

Reputation: 4917

You can achieve that using this:

RewriteEngine On
RewriteRule ^index\.html$ / [R=301,L]
RewriteRule ^(.*)/index\.html$ /$1/ [R=301,L]

The rewrite rule checks for any request that ends with /index.html, and removes the index.html bit if any are found.

Make sure you clear your cache before testing this.

EDIT:

Try this also, this should cover both .html and .php:

RewriteCond %{THE_REQUEST} \/index\.(php|html)\ HTTP [NC]
RewriteRule (.*)index\.(php|html)$ /$1 [R=301,L]

Upvotes: 1

Denix
Denix

Reputation: 2810

The correct .htaccess syntax to obtain it is:

RewriteRule ^/signup(.*)$ /signup/index.html [R=301,L]

It means that both /signup and /signup/ will be redirected to /signup/index.html

Upvotes: 0

Related Questions