replace ampersand with question mark for only one php - .htaccess

This URL 1 is not working URL 2 is working fine. So the condition should be "after byod.php remove ampersand and put question mark" only for byod.php not for other php scrips
1. http://10.168.4.253/byod.php&redirect=www.google.com.sg/search?q=cars........
2. http://10.168.4.253/byod.php?redirect=www.google.com.sg/search?q=cars........
Is this possible using htaccess

Like the previous question that you asked, if you want it only to apply to byod.php you need to specify that in the match:
RewriteRule ^byod\.php&(.*)$ /byod.php?$1 [L,QSA,R=301]

You should probably dig for mod_rewrite or similar tool for you webserver.

Related

Very simple mod_rewrite help

I've got a directory called fb and a script inside called like.php. I'd like to have the get-id passed to the like-file using mod_rewrite.
mypage.com/fb/like.php?id=5 would be mypage.com/fb/like/5
My (not working) htaccess looks like this:
RewriteEngine on
RewriteRule /fb/like/([0-9]+) /fb/like.php?id=$1
Does anyone see what's wrong here?
Try removing the slash at the beginning of your match and replace patterns like so:
RewriteRule fb/like/([0-9]+) fb/like.php?id=$1
mypage.com/ is the domain name, so the string that gets matched is fb/like/5
Also consider using the carat at the start of your match string so that it will match fb but not fffb:
RewriteRule ^fb/like/([0-9]+) fb/like.php?id=$1
Here's a short guide to mod_rewrite I've found helpful.
Edit for your follow-up question:
To match mypage.com/something/fb/like/5, you can do this:
RewriteRule ^([^/]+)/fb/like/([0-9]+) $1/fb/like.php?id=$2
This saves the first directory as $1. [^/]+ means match one or more characters that are not a slash. Put this .htaccess file in the root directory of your domain.
Alternatively, you can use the second-to-last rule and put that .htaccess file in the "something" subdirectory. Hope that makes sense.
Or you can write a rule to match simply like.php/([0-9]+) so that it'll work no matter what the directory path looks like. You can go even more generic and make this apply to any PHP file, not just like.php. It really depends on how you want your site to work.

Url rewriting mod_rewrite

I'm using this tester for url mod_rewrite test:
http://martinmelin.se/rewrite-rule-tester/
When I write something like this:
RewriteRule ^x/([a-z]+)/([0-9]+)/$ x.php?x=$2
Even though I have entered url :
x/p/6/
It get renamed to
x.php?x=p
and. What I want is
x.php?x=6
I'm new with mod rewrite, so anything would be useful.
I'm not a pro in mod_rewrite myself, but I think there is something wrong with that tool.
for example, if you change x.php?x=$2 to x.php?$1=$2, $2 will be parsed correctly.
And then, CMIIW but the regex looks fine. Test the regex here or here.
And Have you ever actually tried the RewriteRule yourself?
RewriteRule ^x/([a-z\/]+?)/([0-9]+)/$ x.php?x=$2
This adds all / characters to the first regex block. The ? makes the search non-greedy.

how do I improve my simple mod_rewrite in htaccess?

I am using mod_rewrite for clean urls but I'm just having a small problem.
The line in particular is:
RewriteRule ^events/(.*)$ fe_events.php?event_url=$1
This works. I can go to url.com/events/something_or_other and it works as expected. The problem however is then if I just go to url.com/events it doesn't work - I've got to go to url.com/events/ (with a trailing forward slash on the end).
Can I simply modify this line or do I need to add another line such as:
RewriteRule ^events$ fe_events.php
Thanks for your time!
RewriteRule ^events(/(.*))?$ e_events.php?event_url=$1
this should work.

problem with .htaccess rewrite rule

I have url's like games/xbox/2
2 being the page number.
some url's are as follows games/xbox-360/2
i need the links to be rewritten.
here is the code i am using which works fine for the links with no - in them
RewriteRule games/([A-Za-z0-9]+)/?$ games/consoles.php?console=$1
RewriteRule games/([A-Za-z0-9]+)/([0-9]+)/?$ games/consoles.php?console=$1&page=$2
what needs to be added to the reg expression to make it also rewrite the links that have the - in them. Thanks
If you add the - right before the ] it will work. But a better method is to escape the - with a \ so you'll end up with [A-Za-z0-9\-]

htaccess: redirect to new url problem

I would like to redirect any php page with optional parameter to a new clean url
eg, from: account.php?id=156 to newurl/account/156
I'm using the following redirectmatch
RedirectMatch ^/cmstut/([a-zA-Z0-9_]+)[\.php]?[\?]?[a-zA-Z_]?[=]?([0-9]+)?$ /cmstut/redirect/newurl/$1/$2/$3 [L]
but the result I get is it will redirect to newurl/account//?id=156
I thought it was funny when I read somewhere where htaccess and regular expression was compared to voodoo :) we'll now I understand why
I don't understand where your third subexpression is.
$1: ([a-zA-Z0-9_]+)
$2: ([0-9]+)
$3: MIA
May I recommend something more like this?
/cmstut/([A-Za-z0-9]+)\.php(\??id=([0-9]+))?
then you may have to use $3 to access the id number. you want the entire parameter to be optional, right?
I never used URL redirects in .htaccess before, but if it's plain regular expressions, this should work:
RedirectMatch ^/cmstut/([a-zA-Z0-9_]+)\.php(\?[a-zA-Z_]+=([0-9]+))?$ /cmstut/redirect/newurl/$1/$3 [L]
Since you weren't very specific in what you want, I took some guesses. This will redirect foo.php?bar=123 to newurl/foo/123 and will ignore bar.
Edit: Thinking about it, rewriting your regexp for you won't help you in the long term, and no one except you is likely to know exactly what you want. I think a better course of action is pointing you to a regexp guide. Here is one, and it's specifically targeted for mod_rewrite.
I finally found the solution. I did some more research and I used a different approach which I believe was even better then what i was using
RewriteCond %{QUERY_STRING} ^id=([0-9]+)$ [NC]
RewriteRule ^([A-Za-z0-9]+)\.php$ /cmstut/redirect/newurl/account/%1? [R=302,L]

Resources