htaccess two parameter mod-rewrite problems - .htaccess

I'm trying to setup a more friendly URL system and failing miserably. I want to be able to pass 1 or 2 GET parameters like this:
http://website.com/1234/123456
where 1234 is the first param and 123456 is the second param.
In my attempts Apache keeps viewing the /1234/ as a folder and a parameter. Here's what I've tried so far:
RewriteRule ^([^/]*)/([^/]*)$ index.php?id=$1&pa=$2 [L]
and
RewriteRule ^(.*)/(.*)$ index.php?id=$1&pa=$2
RewriteRule ^(.*)$ index.php?id=$1 [L]

You could do it like this:
RewriteEngine On
RewriteRule ^/?index\.php$ - [L]
RewriteRule ^/?(?:([^/]*)(?:/([^/]*)/?)?)?$ /index.php?id=$1&pa=$2 [L]
It's a little strict and messy, so you could just try:
RewriteEngine On
RewriteRule ^/?index\.php$ - [L]
RewriteRule ^/?(([^/]*)(/?(.*)))?$ /index.php?id=$2&pa=$4 [L]
That will take everything past the last so host.com/123/1233/56 will rewrite to host.com/index.php?id=123&pa=1233/56 while the first won't rewrite it, because of the 56

Related

ReWrite rule not working with two parametrs

im quite new to rewrite rules.
I can manage with one variable and thats it.
I have webpage Where the rewriterule is:
RewriteCond %{HTTP_HOST} ^www\.someserver\.com$
RewriteRule ^/?$ "http\:\/\/someserver\.com\/" [R=301,L]
RewriteRule ^(\d+)*$ ./index.php?comp=$1
RewriteRule ^(\d+)*/$ ./index.php?comp=$1
And it all work fine as it should. But now as i want 1 more variable into URL
i cant get it to work.
Right now the $1 is only numbers.
someserver.com/1554886
But i want two variable.
someserver.com/1554886-SOMENAME-WHATEVER-WORD-AND-HOW-MANY
But it wont show.
i tried smth like this:
RewriteRule ^([^-]*)-([^-]*)$ ./index.php?comp=$1&string=$2 [L]
How do i get it to work?
Do i have to make some changes in the php side as well?
everything what comes after the number part of the URL is there only for
SEO, the number is Unique
You need one more rule to handle two parameters:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(someserver\.com)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L,NE]
RewriteRule ^(\d+)/?$ index.php?comp=$1 [L,QSA]
RewriteRule ^(\d+)-(.+)/?$ index.php?comp=$1&string=$2 [L,QSA]

.htaccess RewriteRule to remove ? following hostname

I have an htaccess file with some rules, and I now want to add another rule which strips a part of the URL such that
www.mydomain.com/?generations/anything
becomes
www.mydomain.com/anything
(and anything means any other characters). I can make it work without the ?, but I can't seem to match/remove the ?. I tried:
1 failed:
RewriteRule ^\?generations/(.*)$ /$1 [L]
2 failed:
RewriteCond %{QUERY_STRING} ^generations/(.*)$
RewriteRule ^generations/(.*)$ $1 [L]
3 failed:
RewriteCond %{QUERY_STRING} ^generations/(.*)$
RewriteRule ^([^?]*)?generations/(.*)$ $2%1 [L]
4 failed:
RewriteRule ^([^?]*)?generations/(.*)$ $1$2 [QSA,L]
Can someone create a working rule - and explain why my third attempt above is not working? I can potentially see problems with the first two, but the third and fourth should work...
You're getting there with 3, but the query string is not included in a RewriteRule match. So you just want to match the empty string:
RewriteCond %{QUERY_STRING} ^generations/(.*)$
RewriteRule ^$ %1? [L]
But you also have it the wrong way round I think, if you want /anything to be the URL that is visited in the browser. You actually want just this:
RewriteRule ^(.*)$ ?generations/$1 [L]

Im trying to combine three various clean url rules

my problem is that I want to get three simple rules working, but my knowledge is too little, to get them working together:
These are obvious:
RewriteRule ^login$ /login.php [L]
RewriteRule ^register$ /register.php [L]
domain.com/login and domain.com/register
Secondly, since i have only one page used for displaying data, i want its url to be as simple as posible, like:
RewriteRule ^(.*)$ /data.php?id=$1 [L]
which should be translated into:
domain.com/1a2s3d
As third, I want to be able to change url with activation code:
RewriteRule ^register/activate/([^/]+)$ /register.php?action=activate&code=$1 [L]
which finally should be translated into:
domain.com/register/activate/some-hash
I know just simply basics. And I cannot mix all of these three ideas into one single working htaccess file. With the second rule the server gives me 500 error, with third rule registration page works, but css file path is translated into domain.com/register/activate/theme/style.css instead of domain.com/theme/style.css
Any help would be appreciated.
Try just with that:
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^login$ /login.php [L]
RewriteRule ^register$ /register.php [L]
RewriteRule ^register/activate/([^/]+)$ /register.php?action=activate&code=$1 [L]
RewriteRule ^(.*)$ /data.php?id=$1 [L]

Htaccess url rewrite not working correctly

I have an issue with url rewriting, here's what I have so far:
Options +FollowSymlinks
RewriteEngine on
RewriteRule \.(gif|png|jpg|css|js)$|^index\.php$ - [L]
RewriteRule ^users/(.+)$ /profile.php?userid=$1 [NC]
RewriteRule ^(.+)/(.+)$ /main.php?region=$1&place=$2 [NC]
Basically, if the url is mysite.com/users/username I want it to use profile.php, then for anything else, to go to main.php.
However, with that code it doesn't happen as expected, it always uses main.php. Is there anyway you can use if and elses in htaccess, or something similar?
You're missing L flag from 2nd and 3rd rules. Have your code like this:
RewriteRule \.(gif|png|jpg|css|js)$|^index\.php$ - [L]
RewriteRule ^users/(.+)$ /profile.php?userid=$1 [NC,L,QSA]
RewriteRule ^(.+)/(.+)$ /main.php?region=$1&place=$2 [NC,L,QSA]

.htaccess mod_rewrite playing with variables

I want to have my site urls look like
http://example.com/place/info?var=info&morevars=ifneeded
Place and info are also variables but they have a fixed name, the ones after would vary. EDIT This is the url I am trying to rewrite
http://example.com/test.php?place=test&action=info&var=info&morevars=ifneeded
This is what I have so far
RewriteEngine on
RewriteRule ^([A-Za-z0-9-]+)/?$ test.php?place=$1 [NC]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ test.php?place=$1&action=$2 [NC]
I think there a way to do this with {QUERY_STRING} but I can't get it to work just 500 errors or it don't make a differences.
You have set the QSA flag that automatically appends the original requested query to the new one:
RewriteEngine on
RewriteRule ^([A-Za-z0-9-]+)/?$ test.php?place=$1 [NC,QSA]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ test.php?place=$1&action=$2 [NC,QSA]
You're missing the first /
RewriteEngine on
RewriteRule ^/([A-Za-z0-9-]+)/ test2.php?place=$1 [NC]
RewriteRule ^/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/ test2.php?place=$1&action=$2 [NC]

Resources