Janaki
Janaki

Reputation: 145

how to rewrite URL using .htaccess file?

I am having problem in rewriting a url using .htaccess file. I am not aware of regex. I have red so many blogs and used online rewrite rule generator to generate url and pasted in .htaccess file. But no one is worked. I have enabled mod_rewrite module in httpd.conf file in apache server.

I want to rewrite a below url.

I have developed project in OS - Win7 with Apache server.

http://localhost:81/quora/tamilnanban/innerpage.php?url=why-you-should-avoid-watching-porn-on-android-smartphones

to

http://localhost:81/quora/tamilnanban/why-you-should-avoid-watching-porn-on-android-smartphones

my current .htaccess file contains below codes:

RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f

RewriteRule ^.*$ ./index.php
#RewriteRule ^url/([^/]*)\.html$ :81/quora/tamilnanban/innerpage.php?url=$1 [L]

RewriteRule ^/quora/tamilnanban/([^/]*)\.html$ /quora/tamilnanban/innerpage.php?url=$1 [L]

This above rule also not working. Kindly help me to achieve url rewriting using .htaccess file. Also if i am doing any mistakes in the above steps, please correct me. Thanks in advance.

Upvotes: 1

Views: 1809

Answers (1)

Amit Verma
Amit Verma

Reputation: 41219

To convert /quora/tamilnanban/innerpage\.php\?url=foobar" to /quora/tamilnanban/foobar you can use the following rules in /quora/tamilnanban/.htaccess :

RewriteEngine on

#1)Redirect "/quora/tamilnanban/innerpage\.php\?url=foobar" to "/quora/tamilnanban/foobar"#
RewriteCond %{THE_REQUEST} /quora/tamilnanban/innerpage\.php\?url=(.+)\sHTTP  [NC]
RewriteRule ^ /quora/tamilnanban/%1? [L,R]
#2)The rule bellow will internally map "/quora/tamilnanban/foobar"" to "/quora/tamilnanban/innerpage\.php?url=foobar"#
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?(.+)$ /quora/tamilnanban/innerpage\.php?url=$1 [L]

Upvotes: 1

Related Questions