cup_of
cup_of

Reputation: 6687

Add a string to the url using .htaccess file

I need help with redirecting in my .htaccess file. I would like to add a string to whatever is at the end of the url. For example:

When someone goes to this url:

www.mysite.com/example

I would like to rewrite the url to say this:

www.mysite.com/result.html?search=example

So I just need to add the string result.html?search= before whatever is at the end of the URL.

The word example is just a placeholder, I need any word that is put back there to work.

Upvotes: 0

Views: 146

Answers (2)

arkascha
arkascha

Reputation: 42925

Based on your additional comment the question I assume this is what you are actually looking for:

RewriteEngine on
RewriteRule ^/?(.+)/?$ /result.html?search=$1 [END]

If you experience a http status 500 with that rule ("internal server error"), then chances are that you operate a very old version of the apache http server. Try using the [L] flag then instead of the [END] flag. You will find a corresponding hint in your http servers error log file in that case too.

The above rule will work likewise in the http servers host configuration and in a dynamic configuration file (.htaccess style file). Obviously the rewriting module must be loaded and activated in the http server for that. If you decide to use a dynamic configuration file then you also need to make sure that such files are actually considered by the http server. Read about the AllowOverride directive in the documentation for that.

And a general hint: you should always prefer to place such rules inside the http servers (virtual) host configuration instead of using dynamic configuration files (.htaccess style files). Those files are notoriously error prone, hard to debug and they really slow down the server. They are only supported as a last option for situations where you do not have control over the host configuration (read: really cheap hosting service providers) or if you have an application that relies on writing its own rewrite rules (which is an obvious security nightmare).

Upvotes: 1

Pranshu Jain
Pranshu Jain

Reputation: 570

This should be helpful.

RewriteEngine on 
RewriteBase /

RewriteRule ^page/([^/]+)/? result.html?search=$1

Upvotes: 1

Related Questions