4

I have the following situations on my server:

/->
  news-> 
    .htaccess
    index.php
    post.php
...

And the following rules in my .htaccess:

RewriteRule    ^(.*)/admin          post.php?slug=$1&admin_mode=true            [NC,QSA,L]
RewriteRule    ^(.*)$               post.php?slug=$1                            [NC,QSA,L]

Now I need my URLs to be the following:

If requested www.mydomain.com/news/ -> it should get the index.php file

If requested www.mydomain.com/friendly-title-of-my-article -> it should get the post.php file with the query string as indicated in my .htaccess.

Currently I get correctly the post.php with the query string, but when I go to www.mydomain.com/news/ , it's requesting the post.php file.

Please help. Thanks

2 Answers 2

6

Use this

#if query string is empty

RewriteCond %{QUERY_STRING} ^$

#match on / for root, or .index.php in request and send to query string version 

RewriteRule ^(/|index.php)?$ /index.php?step=1 [R=301,L]
Sign up to request clarification or add additional context in comments.

3 Comments

already tried that.. it gets me to example.com/var/www/site/news/index.php?step=1 when requesting www.example.com/news/
this should be the answer. +1 for RewriteCond %{QUERY_STRING} ^$
+1 for (/|index.php), I had no clue I can do that in .htaccess. busted my head for 1 hour trying to redirect when no QUERY_STRING exists.
2

One way to create nice routing is to let everything go to one index.php and control the flow there. It has multiple advantages like being able to query the database and then decide what page to load. That can influence SEO nicely.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.