Please Verify the htaccess code - php

I have a URL like this
http://www.freshupnow.com/movie.php?moviename=A+Flat
I want to rewrite this URL using .htaccess code like this
http://www.freshupnow.com/movies/A+Flat
So, I have used the following code for this
RewriteRule ^movies/([A-Za-z0-9-]+)/?$ movie.php?moviename=$1 [NC,L]
Can Some one please verify it, Either it is Right or Wrong?

([A-Za-z0-9-]+) should be ([A-Za-z0-9+])+ coz you want to capture the whole group 1 or more times and you also want to allow "+" character in your movie names, as in the example "A+Flat". You can validate your regex yourself using this online tool: Regex Validate. There are several others, try Googling for it.

Related

.htaccess regex file

I would like some help regarding regex in .htaccess for url. I have this url I want to represent, I am pretty sure my regex code is accurate but when I load the page it gives me a 404 error. I am not sure but maybe I am missing something. Please help me out if you can.
original:
display-item.php?ic=70efdf2ec9b086079795c442636b55fb-17&n=Cosmetic%20For%20Women
ui has an md5 followed by a - then an id then n accepts a string with spaces
regex:
RewriteRule ^display/(/^[a-z0-9]{32}+\-[1-9][0-9]{0,10}$/)/(^[a-zA-Z0-9\.\s\-]+) view/display-item.php?ic=$1&n=$2

Can't rewrite URL because parameters are dynamic

I'm making an API, everything is handled inside the file, so here's what an example URL might look like.
https://website.com/api/?type=search&user=bob
And I'd want that to turn into
https://website.com/api/search/bob
But now here's the other part to this issue. I have another type, which is CSRF
https://website.com/api/?type=csrf
And that would be
https://website.com/api/csrf/
Note that it's one parameter short, but yet still working off the same file. Anything i've tried never seems to work correctly. Additionally there always seems to be a \ added to the api file. I've already removed the .php from there.
So when I try this it doesn't work. Any ideas?
rewrite ^/([a-zA-Z0-9_-]+)/([0-9]+)$ /api/?type=$1&user=$2;
Your problem seems to be that you use $2 for your username and this correspond to ([0-9]+) in your regular expression.
Which means, username will have to be numbers only.
Change your expression to :
rewrite ^/([a-zA-Z0-9\_\-]+)/([a-zA-Z0-9\_\-]+)$ /api/?type=$1&user=$2;
And your rules should work.

simple mod_rewrite not working

Im kinda new using htaccess and mod rewrite
I got every link to work as i want, but i have a problem when im trying to change the link for the forum everything goes kinda to hell..
This is my code
RewriteRule ^thread/([0-9]+)/([A-Za-z0-9]+)$ sidor/forum/showthread.php?threadID=$1&name=$2 [L]
When i try to get thread/1/forum it works perfectly but when i try to get a longer name like this
thread/2/cs-wont-work-for-me
The htaccess gets me a error 404..
And my links i change so (spaces) gets - and åäö gets aao
Anyone knows what the problem is?
Do you need more code? just post a comment and tell me that then i can try to give you a little more.
thread/2/cs-wont-work-for-me is not matched because you only check for alpha numeric characters ([A-Za-z0-9]+). Include dashes and any other characters you want to match to your regex. This should do what you want:
^thread/([0-9]+)/([A-Za-z0-9\-ÅÄÖåäö]+)$
As a sidenote, I can really recommend htaccess tester to debug issues like this.
You can use as
RewriteRule ^thread/([0-9]+)/(.*) sidor/forum/showthread.php?threadID=$1&name=$2 [L]
Does not check the last sequence of string of specific pattern

htaccess RewriteRule url frendly searches

I have a .htaccess file with this in it:
RewriteRule ^search/([a-zA-Z]+)$ index.php?page=search&search=$1
So basically it sends URLs like this:
url.net/search/this
To this:
url.net/?page=search&search=this
But when I send it a URL like:
url.net/search/this+search
I get an error returned as it doesn't know how to deal with +search bit.
Is there a way I can get it to include the + between words when the user clicks search?
I want it so that if the user types i+want+this+or+that or this+is+what+i+want+to+find, so no mater how long it is, it knows how to handle the parse to $_GET['search'] parameter.
You should be able to just include it in the regex...just remember to escape it,
RewriteRule ^search/([a-zA-Z\+]+)$ index.php?page=search&search=$1
Try this regex for the rewire rule:
RewriteRule ^search/([a-zA-Z].+)$ index.php?page=search&search=$1
Note the . before the + sign. Works as a regex here on this live PHP regex site. Yes, I know this is an Apache rewrite rule & PHP has no role at this stage, but basic regex logic should remain the same.

.htaccess, Rewriting language code to PHP vars

I tried to find some examples here but none of them work for me.. I'm a newbie working with R.E.
I need some help on .htacess > RewriteRule. How could I achieve this?
www.mysite.pt/folder/?a=home&id=22 -> www.mysite.pt/folder/index.php?lang=pt&a=home&id=22
www.mysite.pt/folder/en/?a=home&id=22 -> www.mysite.pt/folder/index.php?lang=en&a=home&id=22
Notes:
both with and without "www"
the ?a=home&id=22 is only an example, I would like to append the
whole query-string.
The 'folder' is needed until I release the website cause I have to
test it on my client server (e.g. www.mysite.pt/site_v2/).
Thanks in advance for your help,
Pedro
I can mainly help you out with regex. I did a few htaccess interventions in the past but don't know the details.
www.mysite.pt/folder/?a=home&id=22 => www.mysite.pt/folder/index.php?lang=pt&a=home&id=22
regex
(www\.)?mysite\.pt/folder/\?(.*)
replace with
www.mysite.pt/folder/index.php?lang=pt&$1
www.mysite.pt/folder/en/?a=home&id=22 => www.mysite.pt/folder/index.php?lang=en&a=home&id=22
regex
(www\.)?mysite\.pt/folder/en/\?(.*)
replace with
www.mysite.pt/folder/index.php?lang=en&$1
I recall that the order you specify the rewrite rules is important and AFAIK first one wins.
Why? It could be that multiple regexes match
This is not the case with these 2 regexes but something to be aware of
Do you want to support only EN and PT?
I also expect that you have a lot of image, CSS, files, etc. Will you want EN and PL versions of those as well? Anyway, let's focus on the scripts for now. Here you want to pick up URIs for /somefolder/en/ and then /somefolder/. If you are using the DOCroot .htaccess then you'll need this:
RewriteEngine On
RewriteBase /
RewriteRule ^(.*?)/en/$ $1/en/index.php?lang=en [L,NS,QSA]
RewriteRule ^(.*?)/$ $1/pl/index.php?lang=pt [L,NS,QSA]
Lookup the L,NS,QSA flags in the mod_rewrite documentation. You'll need the NS flag to stop subqueries biting you. The QSA flag merges the lang parameter with the rest of the query list.
If you want a fixed folder then replace the (.*?) and $1 by the fixed folder name. Also remember that your HTML references will need to work as well. e.g. if a page includes an image src="images/mygif.gif" then this will either resolve to <same directory as HTML file>/images/mygif.gif from the browser perspective, e.g. /folder/en/images/mygif.gif so you'll need to figure out where to map these and add the corresponding rules.

Categories