1

I just started learning about .htaccess , and i can't seem to understand how to redirect to a url ,when the current url doesn't contain something in it and/or is not like it suppose to be.

For example:

Someone is trying to access www.example.com/contact.php . But my htaccess says that every url that is not www.example.com or doesn't contain '?page='(www.example.com/index.php?page=contact.php) , should be changed into www.example.com .

Also this checking must be before this:

RewriteEngine On
        RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
        RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

So whenever , the user types example.com , it will be redirected to www.example.com . In case it writes example.com/contact.php or www.example.com/contact.php , it will be redirected to www.example.com , because it is not www.example.com neither contains '?page=' .

4
  • Your htaccess says none of the things you mention. As shown it will redirect any URL to www.example.com if the host is not www.example.com. The resource /index.php is different from the host address Commented Jul 28, 2015 at 9:05
  • @arco444 , as i said....i just started learning 1 hour ago , and i'm pretty stuck... Commented Jul 28, 2015 at 9:49
  • If I understand, you would like to check if the file exists (contact.php for example), and if not, redirect to the homepage? Commented Jul 28, 2015 at 11:57
  • @zessx , no , i want to check if the url is like www.example.com or like www.example.com/index.php?page=contact.php , in other words if it contains '?page=' (this substring is static in my application) . If the current urls is like those 2,then it's alright,but if they aren't then it should redirect to www.example.com . As an example,if someone access www.example.com/contact.php , it doesn't pass the condition, so it will be redirected . Commented Jul 28, 2015 at 11:59

2 Answers 2

2

I think this piece of code will solve you problem, and helps you to understand a few things with htaccess rules:

# Active rewrite engine
RewriteEngine On

# Force www.
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

# If it's the homepage
RewriteCond %{REQUEST_URI} ^/?$
# Skip the next 4 rules (self-inclusion)
RewriteRule .* - [S=4]

# If url is not "index.php"
RewriteCond %{REQUEST_URI} !^/index.php [OR]
# Or does not contain "page="
RewriteCond %{QUERY_STRING} !(^|&)page=.+(&|$)
# Redirect to homepage
RewriteRule . /? [L,R=301]
Sign up to request clarification or add additional context in comments.

2 Comments

sorry for answering so late , i tried your example,but it doesn't work , it goes into a loop ,and by the way , the url www.example.com/index.php should redirect to www.example.com too , so the first condition is not good.The url is valid if is www.example.com or www.example.com/index.php?page=contact.php . (if contains the '?page=' , string).
nevermind,i managed to make it work,just changed few things,but your code was exactly what i was looking for,thanks a lot.
1

Would you try this, it is really awesome website to convert dynamic links to search engine readable link through htaccess Convert to htaccess file

1 Comment

Good sharing, Great!! :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.