So first, I promise I've looked everywhere but I'm either not finding the right answer or not knowing I found that right answer and moved on (possible). I have some strings that might contain one or more words that are spaced with a + inside my normal page which would look like this http://beaneandco.com/pageTESTING.php?name=Boys+Shirt. This page works fine except it's dirty. I wrote a rewrite code that cleans this up.
RewriteEngine On
RewriteBase /
RewriteRule ^([a-zA-Z0-9])$ pageTESTING.php?name=$1 [QSA,L]
RewriteRule ^([a-zA-Z0-9])/$ pageTESTING.php?name=$1 [QSA,L]
If my page is just one word, as in Boys, it works just fine when shortened. For whatever reason though, when it has two words or more seperated by a + it no longer works. There is a GET at the beginning of my page that has this string if that changes anything. So.........what in the world am I missing?
Thanks in advance.
Add in the regexp code the +, space and other characters you have in URL.
Try this in your htaccess:
RewriteEngine On
RewriteBase /
RewriteRule ^([a-zA-Z0-9_\+ \-])$ pageTESTING.php?name=$1 [QSA,L]
RewriteRule ^([a-zA-Z0-9_\+ \-])/$ pageTESTING.php?name=$1 [QSA,L]
Related
Ahoy all, I know these questions have been beaten to death. And even after my hour of research on stack, none of the suggestions or answers I have attempted have worked.
The only thing I am needing to do is....
Take the variable from a URL and redirect to another page (site), inserting that variable accordingly.
Keep in mind the variable can contain letters, dashes, underscores or numbers.
Original
/directory-here/a-random-name.php?strPropID=13d3-1
and I am redirecting to another domain.
http://www.someotherdomain.com/redirect.php?strPropID=13d3-1
So now I have this:
RewriteRule ^/directory-here/a-random-name.php\?strPropId=(.*)$ http://www.someotherdomain.com/redirect.php?propid=$1 [R=301,L]
However the redirect isn't working. What am I doing wrong here? I know it has to be something simple, no?
Thanks in advance!
You need to match query string using a RewriteCond not in RewriteRule:
RewriteEngine On
RewriteCond %{THE_REQUEST} /directory-here/a-random-name.php\?strPropId=([^\s&]*) [NC]
RewriteRule ^ http://www.someotherdomain.com/redirect.php?propid=%1 [R=301,L,NE]
I'm using this format in my htaccess to redirect several pages/links:
RewriteEngine On
RewriteRule special.php http://www.mysite.com [R=301]
...
...
RewriteRule http://www.mysite.com/special.php?t=master http://www.mysite.com/index.php?q=former [R=301, L]
I noticed, first, that only the top line is catching anything, and in fact the others, like the bottom line, did nothing until I put in that top line. Any ideas why?
Second, mysite.com/special.php?t=grave is redirected, by the above top line, to mysite.com/?t=grave , thus retaining the variables in the URL. I don't want this, I simply want it to go to mysite.com with no variables. How do I do this?
Thanks,
Derek
First, your first rule catches any URI with special.php in it, even if it is followed by a bunch of characters. To limit it to only and exactly special.php, and to make sure the query string is discarded, change it to
RewriteRule ^special.php$ http://www.mysite.com/? [L, R=301]
Secondly, rewrite rules only match the part after http://www.mysite.com/ (note the last slash) and before the query string (the part after the question mark). So if you change the format of those rules to
RewriteCond %{QUERY_STRING} t=master
RewriteRule ^special.php$ index.php?q=former [R=301, L]
you should be good to go.
I have the following code in the my .htaccess file and the top Rewrite works fine the bottem one does not I know why but I dont kno how to fix it.
Its seeing RewriteRule ^([^/]*).html index.php?p=order&course_id=$1 [L] as the top rewrite command becuase of the hightlighed part and i dont want to put it in a dir
RewriteEngine on
RewriteRule ^([^/.]+).html
index.php?p=$1 [L]
index.php?p=about_us
RewriteRule ^([^/]+).html
index.php?p=order&course_id=$1 [L]
index.php?p=order&course_id=5
Thank you,
Can you give example urls that should match the pattern block and what you would like them to be rewritten to? That would be very helpful.
One thing I notice is that your first regexp you test if you match the pattern block with a + which means 1 or more times and the second one you check it with a * which means 0 or more so I don't think the second one will ever be called, although I am very new to regexps but it is just something I noticed.
These are very helpful resources for me:
http://www.webforgers.net/mod-rewrite/mod-rewrite-syntax.php
http://forum.modrewrite.com/
From the example of the urls you would be using, this should work:
# http://website.com/about_us/ rewrites to /index.php?p=about_us
RewriteRule ^([0-9a-z_-]+)/?$ index.php?p=$1 [NC,L]
# http://website.com/order/12/ rewrites to /index.php?p=order&course_id=12
RewriteRule ^order/([0-9]+)/?$ index.php?p=order&course_id=$1 [NC,L]
The second Rewrite might be:
# http://website.com/order/12/ rewrites to /index.php?p=order&course_id=12
RewriteRule ^([0-9a-z_-]+)/([0-9]+)/?$ index.php?p=$1&course_id=$2 [NC,L]
Depending on your page structure.
I would like to know how to allow periods at the end on my url's. Currently if I had the url:
http://www.mydomain.com/Page/I.D.K.
The page grabbing the information would return
Title: I.D.K (Without the ending period)
This also happens with other punctuation and it is effecting my pages displaying information wrongly. Thanks for looking, hope somebody knows the solution.
I am using the following in my htaccess file:
RewriteRule ^([^/.]+)/?$ /index.php?Page=$1 [L]
RewriteRule ^([^/.]+)/([^/]+)/?$ /index.php?Page=$1&Level1=$2 [L]
RewriteRule ^([^/.]+)/([^/]+)/([^/]+)/?$ /index.php?Page=$1&Level1=$2&Level2=$3 [L]
RewriteRule ^([^/.]+)/([^/]+)/([^/]+)/([^/]+)/?$ /index.php?Page=$1&Level1=$2&Level2=$3&Level3=$4 [L]
Ok problem virtually solved. The & still breaks my strings if I try doing Artist1 & Artist2 but I will just use string replace to replace that symbol with something else. Anyway, to allow punctuation anywhere, even at the end, I replaced all + with * in my rewrite rules. Here is how it looks now:
RewriteRule ^([^/.]*)/?$ /index.php?Page=$1 [L]
RewriteRule ^([^/.]*)/([^/]*)/?$ /index.php?Page=$1&Level1=$2 [L]
RewriteRule ^([^/.]*)/([^/]*)/([^/]*)/?$ /index.php?Page=$1&Level1=$2&Level2=$3 [L]
RewriteRule ^([^/.]*)/([^/]*)/([^/]*)/([^/]*)/?$ /index.php?Page=$1&Level1=$2&Level2=$3&Level3=$4 [L]
Hope this helps some other people with similar problems.
I'm assuming this is located in http://www.mydomain.com/. Here's what I'd use for the second Rule (which can then be transposed to the other rules):
RewriteRule ^([A-Za-z0-9\-,]+)/([A-Za-z0-9.\-,]+)/?$ /index.php?Page=$1&Level1=$2 [L]
That will match the following: (and will allow for hyphens, commas, and periods for Level1)
IDK/Jump.ing.
IDK/Jump.ing./
But not:
S$!LV/J
I try and whitelist my options (something like /[A-Z]/), rather than blacklist them (/[^3-5]/). I'm sure there are arguments for both options, but I like whitelisting mostly because I have control over what can be used. If I don't include it in my list, it's not a valid option. Of course, if you're accepting a LOT of options, then this could make for some very long character classes or whatnot. What if someone went to http://www.mydomain.com/Page/I.D.K.&somevariable=somevalue ? It could potentially break your script (if the person is smart). Your RewriteRule would send them to /index.php?Page=Page&Level1=I.D.K.&somevariable=somevalue. Not at all what you anticipated happening.
I am trying to write a rule which says
xyz.com/hotels_in_someplace will direct to xyz.com/test.php?place=someplace
by using the following in my htaccess
RewriteEngine On
RewriteBase /
RewriteRule ^hotels_in_([a-z]+)$ test.php?place=$1 [L]
but its not working and I cant figure out why, I am running a wamp server as a dev. when i tried to run xyz.com/test.php?place=someplace (without the htaccess in the directory) it works but I suspect there's something my rule which is wrong.
additional:
whoops....my dumb ass mistake, mod_rewrite wasnt enabled...
Try it without the end-line anchor ($)
RewriteRule ^/hotels_in_([a-z]+) /test.php?place=$1 [L]
Also, did you allow the .htaccess files to be parsed?
Update: I didn't notice the RewriteBase in the question before so my point about the leading slash is null. However the RewriteRule does work on my machine.
Try:
RewriteRule ^hotels_in_([a-z]+)$ /test.php?place=$1 [L]
The path to test.php needs the leading slash. The way you are doing the rewrite you will be redirected to xyz.com/path/to/web/docs/on/machine/test.php and thats not what you want. :)
Also if you are you including a trailing slash in your test.
The above rule will not match xyz.com/hotels_in_someplace/.
To match the leading slash this should work:
RewriteRule ^hotels_in_([a-z]+)/?$ /test.php?place=$1 [L]
Your example works fine for me. Might want to make a few little adjustments (see my example, which captures uppercase letters, hyphens and underscores (for hotels in countries with more than one word (united-states, united_states)) and a trailing forward slash).
RewriteRule ^hotels_in_([A-Za-z_-]*)(/?)$ /test.php?place=$1
Hope that helps.