I'd like to rewrite two things on one site.
mysite.com/something -> mystie.com/index.php?s=something
and
mysite.com/something/another -> mysite.com/index.php?s=something&d=another
This is my htaccess
RewriteEngine on
RewriteRule ^(.+)$ index.php?s=$1 [L,QSA]
RewriteRule ^(.+)/(.+)$ index.php?s=$1&d=$2 [L,QSA]
Separately both work but together they don't...
I'm guessing the problem is that you're matching the '/' character in your first rule. Wouldn't the easiest solution be to simply add a character class to your rules, like:
RewriteRule ^([a-z0-9]+)$ index.php?s=$1 [L,QSA]
RewriteRule ^([a-z0-9]+)/([a-z0-9]+)$ index.php?s=$1&d=$2 [L,QSA]
or simply change the order of the rules:
RewriteRule ^(.+)/(.+)$ index.php?s=$1&d=$2 [L,QSA]
RewriteRule ^(.+)$ index.php?s=$1 [L,QSA]
Reverse the two rules. The rewrite engine goes through the file line by line, sequentially. The first rule you have:
RewriteRule ^(.+)$ index.php?s=$1 [L,QSA]
matches a url that has two name value pairs as well, so the file stop processing after the first match because you are using the L flag.
RewriteRule ^(.+)/(.+)$ index.php?s=$1&d=$2 [L,QSA]
RewriteRule ^(.+)$ index.php?s=$1 [L,QSA]
I don't have time to test this for you, sorry. But I'm feeling strongly that this is the problem. While I was writing this answer, someone gave pretty much the same advice. I thought of deleting my answer but want to make sure that you understood about the L flag and that the file is processed sequentially.
RewriteEngine on
RewriteRule ^([a-z0-9]+)$ index.php?s=$1 [L,QSA]
RewriteRule ^([A-Za-z0-9]+)/(.+)$ index.php?s=$1&d=$2 [L,QSA]
This works. Changing order didn't work. Also I forgot add that
mysite.com/something/another
the another string can be like asd-1231-ASDwqda .
Related
Pretty straightforward - just wondering how do I rewrite this:
/folder/subfolder/file
/?f=folder/subfolder/file
And the number of folders/subfolders might vary, so what I really want is anything after the / in that path (that does not already have ?f= in it) redirected to the path with ?f= added in front of it.
If I add this
RewriteRule ^(.*)$ ?f=$1 [L]
it works just fine but doesn't recognize the existence of ?f= in the URL already, and breaks that existing link with ?f= in there.
Any ideas?
You can use:
RewriteCond %{QUERY_STRING} !(?:^|&)f= [NC]
RewriteRule ^(.*)$ ?f=$1 [L,QSA]
Which does not do the rewriting if the f querystring is already there.
With [QSA] you keep the other possible querystring values.
Try this :
RewriteEngine On
RewriteRule ^(.[^\?]*)$ /?f=$1 [L]
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]
RewriteEngine On
RewriteRule ^([a-z-0-9]+)$ linkovi.php?code=$1 [L,QSA]
RewriteRule ^([a-z-0-9]+)$ index.php?code=$1 [L,QSA]
How to enable both of these only 1st one work which ever it is.
Both rules have the same pattern (([a-z-0-9]+)). Apache cannot guess what you want. You are better off doing something like this:
RewriteEngine On
RewriteRule ^linkovi/([a-z-0-9]+)$ linkovi.php?code=$1 [L,QSA]
RewriteRule ^([a-z-0-9]+)$ index.php?code=$1 [L,QSA]
Where the first rule is clearly different, matching requests to linkovi/123, for example.
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]
I have a htaccess setup like so:
RewriteRule ^([A-Za-z_]+)$ profile.php?name=$1 [NC,B,QSA]
it works fine however it doesn't work when the parameter has a number in it.
also is it possible to allow periods and commas?
What can I change?
Change the regex:
RewriteRule ^([0-9A-Za-z_]+)$ profile.php?name=$1 [L,QSA]
OR even better
RewriteRule ^(\w+)/?$ profile.php?name=$1 [QSA,L]
Since \w is same as [0-9A-Za-z_]
OP's comment:
is there a way to let it use periods and commas as well?
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w,.]+)/?$ profile.php?name=$1 [QSA,L]