using ([0-9]+) after (.*) in htaccess rewrite rules - php

this is my code
RewriteRule ^film-(.*)-p([0-9]+)$ cat.php?n=$1&page=$2 [L]
but the problem is the script makes all what is after (.*) like a variable and ([0-9]+)
= nothing. Please help, I need to finish this project.

How about using “([^-]*)“ instead of “(.*)“?
^film-([^-]*)-(p[0-9]+)$

the probleme is solved
my code was
RewriteRule ^film-(.*)$ cat.php?n=$1 [L]
RewriteRule ^film-([^-]*)-p([0-9]+)$ cat.php?n=$1&page=$2 [L]
i changed to
RewriteRule ^film-([^-]*)-p([0-9]+)$ cat.php?n=$1&page=$2 [L]
RewriteRule ^film-(.*)$ cat.php?n=$1 [L]
and now it's works thanks

Related

htaccess rewrite parameter name to "nothing"

I used the follow rule to rewrite example.com/user-beavis to example.com/profil.php?user=beavis
RewriteRule ^([^\-]*)user-(.*)$ $1profil.php?user=$2 [QSA]
But I want it to be example.com/beavis so I tried:
RewriteRule ^([^\-]*)(.*)$ $1profil.php?user=$2 [QSA]
but get 500 Internal Server Error
Is it even possible with any rule?
If you want to convert example.com/beavis to example.com/profil.php?user=beavis, use this:
RewriteRule ^(?!profil.php)(.*)$ /profil.php?user=$1 [L] [L]
I used a negative lookahead to protect from eternal loop.
This is the solution:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ profil.php?user=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ profil.php?user=$1

Mod Rewrite and Arabic characters

I currently have the following rules in my .htaccess file that work perfectly for the English site.
RewriteEngine On
RewriteRule ^index.html$ index.php [L]
RewriteRule ^/?([0-9a-zA-Z_-]+)/?$ rewrite.php?param1=$1 [L]
RewriteRule ^/?([0-9a-zA-Z_-]+)/([0-9a-zA-Z_-]+)/?$ rewrite.php?param1=$1&param2=$2 [L]
RewriteRule ^/?([0-9a-zA-Z_-]+)/([0-9a-zA-Z_-]+)/([^/]+)/?$ rewrite.php?param1=$1&param2=$2&param3=$3 [L]
But, on the Arabic website, this throws a "404" error.
Examples that work:
http://www.mysitedomain.xyz/work/website/pages/goals-and-objectives
http://www.mysitedomain.xyz/work/website/solutions/banking
Examples that DO NOT work:
http://www.mysitedomain.xyz/work/website/pages/الاهداف
http://www.mysitedomain.xyz/work/website/solutions/بنوك
Any idea how to fix that?
Thanks
Given your URL layouts, I don't think you really need to be so specific. You probably just want to split on slashes:
RewriteEngine On
RewriteRule ^index.html$ index.php [L]
RewriteRule ^/?([^/]+)/?$ rewrite.php?param1=$1 [L]
RewriteRule ^/?([^/]+)/([^/]+)/?$ rewrite.php?param1=$1&param2=$2 [L]
RewriteRule ^/?([^/]+)/([^/]+)/([^/]+)/?$ rewrite.php?param1=$1&param2=$2&param3=$3 [L]
in place of ([0-9a-zA-Z_-]+) this utilize (.*), for any content

How to rewrite somedomain.com/free/single/result_id to somedomain.com/f/single/result_id

I'm having trouble getting my head around rewrite.
I'm trying to make it so that the URL somedomain.com/f/single/1 would grab the result from somedomain.com/free/single/1
I've tried the following.
RewriteRule ^/f$ /free$1 [L]
Here is a copy of my existing .htaccess with a suggestion made by Kamil...
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
RewriteRule ^f/(.+)$ /free/$1 [L]
I wonder if the problem is that I am already removing index.php before the /free ?
Can anyone help me understand what I'm doing wrong?
You look for following rule:
RewriteRule ^f/(.+)$ /free/$1 [L]
The first part ^f/(.+)$ is regular expression - it matches all strings starting with /f/ and stores the rest to the $1... then it's rewritten to /free/ and the rest stored in $1.

htaccess With a different amount of variables

I have a website that on one specific page it requires an extra variable.
At the moment, the htaccess I am using is:
RewriteEngine On
RewriteRule ^([^/]*)/$ /index.php?page=$1 [L]
Which works fine with one variable.
How would I get it to work with :
http://tessite.com/index.php?page=test&id=1
So it looked like:
http://tessite.com/test/1
Thanks for any help or replies.
It can be coded like this:
RewriteRule ^([^/]*)/([^/]*)/$ index.php?page=$1&id=$2 [L]
RewriteEngine On
RewriteRule ^(.*)/(.*)/$ /index.php?page=$1&id=$2 [L]
RewriteRule ^(.*)/$ /index.php?page=$1 [L]
Just add $2!
Also, [^/] isn't required, you can simply use ..

mod rewrite with get requests

I am having issue passing get variables.
index?p=calendar refers to calendar.php located in pages/calendar.php and index.php is in root.
my URL is localhost/researchportal/calendar/11/2011
Calendar has 2 get variables, month and year. i.e calendar.php?month=11&year=11
here is my rule, but its not working.
RewriteRule ^calendar/([0-9]+)$/([0-9]+)$ index.php?p=calendar&month=$1&year=$2 [L]
I also tried
RewriteRule ^calendar/([0-9]+)$/([0-9]+)$ pages/calendar.php?month=$1&year=$2 [L]
.htaccess file
RewriteEngine On
RewriteBase /researchportal/
RewriteRule ^/calendar/([0-9]+)$ index.php?p=calendar [QSA,L]
RewriteRule ^users/login /researchportal/pages/login.php [L]
RewriteRule ^users/logout /researchportal/pages/logout.php [L]
RewriteRule ^users/register logout.php [L]
RewriteRule ^profile/([0-9]+)$ index.php?p=profile&usr_id=$1 [QSA,L]
RewriteRule ^profile/edit/([0-9]+)$ index.php?p=edit&usr_id=$1 [L]
RewriteRule ^([A-Za-z0-9-_]+)$ index.php?p=$1 [L]
^calendar/([0-9]+)$/([0-9]+)$
Why are you ending ( $ ) it twice?
^ starts and expression and $ ends it. There is no reason to have neither in the middle of an expression.
RewriteRule ^calendar/([0-9]+)/([0-9]+)$ pages/calendar.php?month=$1&year=$2 [L]
Should work better. If you for some reason would also want some query variables in the url, you can replace [L] with [L,QSA] aswell.
I think the problem is because you have an additional $ character.
RewriteRule ^calendar/([0-9]+) *$* /([0-9]+)$ index.php?p=calendar&month=$1&year=$2 [L]

Categories