2

I am new to php. My .htaccess file is below

DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteBase /
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php?qa-rewrite=$0&%{QUERY_STRING} [L]
</IfModule>

With this the following url formats are working fine

  • /?qa=123/why-do-birds-sing
  • /?qa=123&qa_1=why-do-birds-sing
  • /index.php?qa=123&qa_1=why-do-birds-sing

But i want to make in to the below format

site.com/123/why-do-birds-sing 

I tried adding the below line

RewriteRule ^.*$ index.php/qa-rewrite=$0&%{QUERY_STRING} [L]

But the site.com/123/why-do-birds-sing is not working. What changes should i make to get the desired url format working?

P.S site.com/index.php/123/why-do-birds-sing is working, but i want to rewrite without index.php

3
  • site.com/123/why-do-birds-sing works for me using above rule. What error are you getting? Commented Sep 8, 2015 at 7:36
  • 'm getting 404 error. It works fine when i goto site.com/index.php/123/why-do-birds-sing Commented Sep 8, 2015 at 7:51
  • Verify whether your .htaccess is enabled or not, by putting same garbage (random) text on top of your .htaccess and see if it generates 500 (internal server) error or not when you visit your page in browser? Commented Sep 8, 2015 at 11:22

1 Answer 1

1

The way rewrite rules work is they take whatever is/are in the parameter(s) and reconstructs the url to a more readable and user friendly url. While at the same time passing the parameters (secretly) to your page for use.

You can then access the parameters on the receiving page normally with $_GET['my-parameter']

Below is the rewrite rule you need.

In this part ^([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)$ it writes everything before the slash as your first parameter and everything after the slash as your second parameter. Allowing for any mixture of uppercase letters, lowercase letters, numbers 0-9, underscores & hyphens.

RewriteRule    ^([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)$    index.php?qa=$1&qa_1=$2    [NC,L]

Then construct your links like this... 123/why-do-birds-sing

Your url will look like http://example.com/123/why-do-birds-sing

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @kuya in the first line I have DirectoryIndex index.php, so this means that it will only rewrite when url has index.php in it?
The line DirectoryIndex index.php has nothing to do with rewrites. That line is used only if you want to use something other than index.php as your index page on the site. For example (since linux is case sensitive) you might want to use Index.php as your index page throughout the site. In which case you can use DirectoryIndex to make the change. If you're going to use index.php as your index page, the line DirectoryIndex index.php can be deleted. It doesn't change anything since index.php is already default index page.

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.