0

I am configuring the .htaccess for a website.

I would like to rewrite the url from

http://www.example.com/search/foo/bar/baz/... (any number of subdirectories)

to

http://www.example.com/forms/index.php?i=foo--bar--baz--...

Another option would be to redirect all urls

http://www.example.com/search/foo/bar/baz/...

to

http://www.example.com/forms/index.php

And have index.php parse the uri.

I tried the following mod_rewrite to implement the 2nd alternative above

RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule !^index\.php$ /forms/index.php 

but I get a 404 error.

Note: not sure if it matters, but this is happening on a Wordpress site. The whole .htaccess is

Options +FollowSymLinks RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule !^index\.php$ /forms/index.php # BEGIN WordPress <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule> # END WordPress 

Any help would be appreciated.

Thanks.

EDIT1:

I made some progress. I was missing [L] at the end of the RewriteRule.

RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule !^index\.php$ /forms/index.php [L] 

Works. Now I need to check for /search in the url.

EDIT2:

More progress:

RewriteEngine On RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^search/(.*)$ http://www.example.com/forms/index.php?i=$1 [QSA,L] 

it seems to be working. Passing everything after search/ as parameter i.

Now I would like to still show the original url in the address bar (not the re-written one). Any way of doing that?

Thanks

2 Answers 2

1

Something to the effect of
Rewrite ^(.*)$ forms/index.php?i=$1 [QSA,L]
should work

The key points here is ^(.*)$ select and capture any url, ?i=$1 place the old url as a query string. QSA makes the old query string get appended on incase other information was being passed. You might need this, you might not.

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

1 Comment

that works well. I ended up with RewriteRule ^search/(.*)$ http: //www.example.com/forms/index.php?i=$1 [QSA,L] which seems to be working. Anyway to redirect without changing the URL in the address bar? Thanks.
1

If you're getting 404 error, then you probably don't have rewrite module on, or .htaccess file is not being read at all.

This doesn't seem to be correct:

RewriteRule !^index\.php$ /forms/index.php 

Rewrite engine hierarchy is that first found rule applies, other are skipped. So if you would like not to rewrite index.php, but rewrite all other, you can simply do what Wordpress does:

First line: RewriteRule ^index\.php$ - [L] <- rewrite index.php to "nothing"

Second line: RewriteRule .* /index.php [L] <- rewrite everything to index.php

So when the request is index.php, Apache acts like this: it checks first line... "wow! matches! so lets rewrite it to nothing and load this file... other rules in file? oh well... i don't have time to read them!"

When the request is not index.php, Apache checks first line... "oh well.. does not match... damn... I have to read other lines". Checks second line... "yes! it matches! I don't have to read anything else, phiew!"

1 Comment

thank you for the explanation. It helped me better understand how it works.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.