.htaccess Redirect Rule for & Character - apache

Im not experienced with .htaccess files. I apologize for misconceptions in advance.
If have the following rule in my .htaccess file to accept requests without a filename extension:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^index/([0-9a-zA-Z_-]+)/([öäü0-9a-zA-Z_-]+) index.php?id=$1&term=$2 [NC,L]
An URL like domain.com/index/2/jonathan should be (internally) redirected to domain.com/index.php?id=2&term=jonathan. This works fine except for terms which include the & character.
Adding the & character to the rule like this didnt help:
RewriteRule ^index/([0-9a-zA-Z_-]+)/([öäü0-9a-zA-Z_-&]+) index.php?id=$1&term=$2 [NC,L]
How can I include the & character correctly inside the rule?
I appreciate any help.

Thanks, I was able to solve the problem using the B flag:
RewriteRule ^index/([0-9a-zA-Z_-]+)/([öäü&0-9a-zA-Z_-]+) index.php?id=$1&term=$2 [B,NC,L]

Related

how to dynamically rewrite filepath .htaccess based on domain?

I have this apache rule in my .htaccess file:
RewriteCond %{HTTP_HOST} ^testURL.localhost.local
RewriteRule ^index.php$ /_testURL/index.php [L]
How would I write this so that testURL2.localhost.local or any other subdomain would be rewritten to the corresponding directories?
For instance testURL2.localhost.local would be rewritten to _testURL2/index.php etc
I already tried the option below but I didn't get the intended result:
RewriteCond %{HTTP_HOST} ^testURL
RewriteRule ^cms.php$ /_%1/cms.php [L]
With your shown samples, attempts; please try following htaccess rules file. I have posted 2 sets of htaccess rules file here, you have to use ONLY one set at a time.
1st solution: This is specifically for host testURL.localhost.local and will look for either index.php OR cms.php only in UI.
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^(?:www\.)?(testURL)\.localhost\.local$ [NC]
RewriteRule ^((?:index|cms)\.php)$ _%1/$1 [NC,L]
2nd solution: A Generic solution, where it will look for anyvalue.localhost.local and it will add anyvalue in path while rewriting, also it will look for any php files in uri.
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^(?:www\.)?([^.]*)\.localhost\.local$ [NC]
RewriteRule ^([^.]*\.php)$ _%1/$1 [NC,L]
NOTE1: Also please make sure to clear your browser cache before testing your URLs.
NOTE2: Please keep these rules at the top of your htaccess rules file.
NOTE3: I am also additionally/optimally matching www. in host in case you don't want it you could remove it (?:www\.)? part in condition.

.htaccess rewrite urls with parameters

I am trying to rewrite my urls through a .htaccess file to make them more clean looking. I have
http://localhost:801/Test/test.php?school=19&name=Greenhaven-Elementary
and it needs to end up looking like
http://localhost:801/Test/test.php/19/Greenhaven-Elementary
In my .htaccess file I have the following
RewriteEngine On
RewriteRule ^([a-zA-Z0-9-/+]+)([0-9]+)$ test.php?school=/$1&name=$2/ [L]
I have tried other ways but being new at using .htaccess files I haven't been able to figure it out.
This should do what you're after:
RewriteEngine On
RewriteCond %{QUERY_STRING} school=(.+)&name=(.+) [NC]
RewriteRule ^(.*)$ http://localhost:801/Test/test.php/%1/%2? [R=301,NC,L]
So what does the above do?
First, it will take the query school= and name= as a condition, if this condition is met then it will grab any version of the variables using (.+).
It will then rewrite the URL using 301 redirection to show http://localhost:801/Test/test.php/anything/anything2. The use of %1 and %2 is to grab the variables from school= / name= and then we use ? to stop the original query string from appearing on the end of the newly rewritten URL.
Make sure you clear your cache before testing this.
EDIT:
I wrote this for the singular query:
RewriteEngine On
RewriteCond %{QUERY_STRING} item=(.+) [NC]
RewriteRule ^(.*)$ http://localhost:801/Test/%1? [R=301,NC,L]
This includes removing test.php and on my server works without issue and returns http://localhost:801/Test/anything

.htaccess rewrite this/kind/of/address to this_kind_of_address.php

Hi stackoverflow users!
I need to set an url rewrite via access to make these url requests:
this/kind/of/url
or
this/kind/of/url.php
point to this file:
this_kind_of_url.php
Basically I need to convert all "/" that I get in the url to a "_" and add a .php extension if not provided. I thought it was not too complicated at first but now I'm almost losing my head on it since I'm quite new to .htaccess.
Thanx in advance for your precious help.
Try:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)$ /$1_$2 [E=SLASH:Y,DPI]
RewriteCond %{ENV:SLASH} Y
RewriteRule ^([^/]+?)(?:\.php)$ /$1.php [L,R=301]
It's a little tricky to get your head around, but the first rule checks to make sure the request isn't for an existing file or directory. And if there' a slash in it, then replace it with a _ and set the environment variable "SLASH". This rule will loop until there are no more /'s in the URI.
That's where the second rule comes into play. If the environment variable is set, and there are no more slashes in the URI, then it redirects the browser and adds a .php to the end if there isn't one.
So:
/this/kind/of/url
is redirected to
/this_kind_of_url.php

htaccess mod_rewrite RewriteRule

I am trying to set up an .htaccess file to convert an incoming link like:
http://domain.com/root/TopNav/SubNav/SEO-friendly-file-name-p#
into this:
http://domain.com/root/index.php?t=TopNav&s=SubNav&l=SEO-friendly-file-name&p=#
where p# is the page id and TopNav/SubNav represent the navigation menu path to the file
I have been able to get it to work in all cases except for when there are arguments after the .php (it does the mod rewrite, but loses the parameters). Originally, I was hoping to have the .htaccess parse the url string so that it was ready for the script to use, but at this point I would be happy with any solution that takes the incoming url and dumps it as a string onto root/index.php.
here's what I currently have in the .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^(.*)/(.*)\.php index.php?s=$1&p=$2 [L]
RewriteRule ^(.*)/ index.php?p=$1&s=$1 [L]
</IfModule>
ErrorDocument 404 /404.php
Any thoughts on what I might be doing wrong? Suggestions of a better way to get this done?
Thanks
** someone suggested changing the [L] to [L,QSA] and that seems to have worked. Thanks, whoever suggested that...
With a URU that looks like this: /TopNav/SubNav/SEO-friendly-file-name-p# you've got 4 groupings you need:
TopNav
SubNav
SEO-friendly-file-name
#
So you need to craft your regex so that it captures these 4 things in one go.
Try:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/(.+?)-p([0-9]+)$ index.php?t=$1&s=$2&l=$3&p=$4 [L,QSA]
This would go in the htaccess file in your /root directory

Apache mod_rewrite going berserk - redirecting where it shouldn't

I have a script that echoes a meta redirect to a page called account_management.php5, but for some reason it automatically redirects from there to index.php5. My .htaccess file handles a couple of redirects automatically, for example index.html|php5 to the domain root, and that's the only place I can see this problem originating, but I don't understand why. This is my .htaccess file:
RewriteEngine On
#remember to change this to aromaclear
RewriteCond %{HTTP_HOST} !^sinaesthesia\.co.uk$ [NC]
RewriteRule ^(.*)$ http://sinaesthesia.co.uk/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^GET\ .*/index\.(php5|html)\ HTTP
RewriteRule ^(.*)index\.(php5|html)$ /$1 [R=301,L]
#translate any .html ending into .php5
RewriteRule ^(.*)\.html$ /$1\.php5
#change / for ?
RewriteRule ^(.*)\.html/(.*)$ /$1\.html?$2
#strip .html from search res page
RewriteRule ^(.*)search/(.*)$ /$1search_results\.html/search=$2
#translate product details link from search res page
RewriteRule ^products/(.*)/(.*)/(.*)$ /product_details.php5?category=$1&title=$2&id=$3 [L]
#Translate products/psorisis/chamomile-skin-cream-P[x] to productview.php5?id=1
RewriteRule ^products/.*-P([0-9]+) /productview.php5?id=$1 [L]
Wrong:
RewriteRule ^(.*)\.html$ /$1\.php5
Right:
RewriteRule ^(.*)\.html$ /$1.php5
Righter:
RewriteRule ^(.*)\.html$ /$1.php5 [QSA]
This same mistake of escaping special chars in the second param of RewriteRule is happening in other rules too, I don't know if apache will handle it, but I know you don't need it because second param is not a regexp.
Never compare to %{THE_REQUEST}, thats a weird thing to do, you don't need that. Moreover, this condition is fine without it. Just put there:
RewriteRule ^(.*)index\.(php5|html)$ $1 [R=301,QSA,L]
Now look at it:
RewriteRule ^(.*)\.html/(.*)$ /$1.html?$2
First, you are still accepting that there are references to .html files, just after trying to translate all .html to .php5, there's something wrong here.
Moreover, you are defineing as QueryString something that was originally a file path, and are not even putting it in a key. It won't work, it need some more treatment.
#strip .html from search res page
RewriteRule ^(.*)search/(.*)$ /$1search_results.html/search=$2
Wasn't it supposed to strip the .html? Because it is actually putting a .html there. Maybe as it is not an [L] it get fixed in the next loop, but you could just get all fixed right here.
#translate product details link from search res page
RewriteRule ^products/(.*)/(.*)/(.*)$ /product_details.php5?category=$1&title=$2&id=$3 [L]
This one full of .* is potentially unstable, specially delimitating the end. You should do this:
RewriteRule ^products/([^/]*)/([^/]*)/([^/]*) /product_details.php5?category=$1&title=$2&id=$3 [L]
# or:
RewriteRule ^products/(.*?)/(.*?)/([^/]*) /product_details.php5?category=$1&title=$2&id=$3 [L]
The last one looks correct, except that you should strip the special character that may be faced as a range delimiter, the "-". I don't think it work after a *, but just to be sure and correct the syntax:
RewriteRule ^products/.*\-P([0-9]+) /productview.php5?id=$1 [L]
Add this just after RewriteEngine on
RewriteLogLevel 9
RewriteLog /tmp/rw.log
Then restart the webserver. It should help you debug the problem.
Edit: Sorry, I didn't notice the .htaccess above. This will only work from the main apache configuration file.