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.
Related
i'm trying to create multiple rewrite rules to make friendly URL but what i did, makes my website throw error 500.
I've tried this but can't seem to make it work.
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*) /index.php?category=$1 [L]
RewriteRule ^(.*)/(.*) /index.php?category=$1&subcategory=$2 [L]
RewriteRule ^(.*)/(.*)/(.*) /index.php?category=$1&subcategory=$2&userid=$3 [L]
What i need is basically to make this urls work:
domain.com/GetAnnouncements as domain.com/index.php?category=GetAnnouncements
domain.com/Persona/GetAchievements/2 as domain.com/index.php?category=Persona&subcategory=GetAchievements&userid=2
and there also should be third option that works 'in between' without 3rd parameter which is &userid=2
With your shown samples please try following .htaccess rules file.
Make sure to use either 1st OR 2nd solution only at a time.
Please make sure:
To keep your .htaccess rules file, index.php file in your root location.
Clear your browser cache before testing your URLs.
1st solution: Generic rules where using regex.
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([^/]*)/?$ /index.php?category=$1 [L]
RewriteRule ^([^/]*)/([^/]*)/?$ /index.php?category=$1&subcategory=$2 [L]
RewriteRule ^([^/]*)/([^/]*)/(/d+)/?$ /index.php?category=$1&subcategory=$2&userid=$3 [L]
OR 2nd solution: Using specific string/URLs only as per your shown samples.
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(GetAnnouncements)/?$ /index.php?category=$1 [NC,L]
RewriteRule ^(Persona)/(GetAchievements/)/?$ /index.php?category=$1&subcategory=$2 [NC,L]
RewriteRule ^(Persona)/(GetAchievements/)(/d+)/?$ /index.php?category=$1&subcategory=$2&userid=$3 [NC,L]
After some more googling and consulting with my friend we came to this solution which works:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*)/(.*)/(.*)$ index.php?category=$1&subcategory=$2&userid=$3 [L,QSA]
RewriteRule ^(.*)/(.*)$ index.php?category=$1&subcategory=$2 [L,QSA]
RewriteRule ^(.*)$ index.php?category=$1 [L,QSA]
Thank you everyone who tried to help!
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]
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 .
On my blog (which I'm in the process of redesigning for a more efficient way of displaying content), I have a RewriteRule setup to handle a simple redirection from
http://new.kn3rdmeister.com/blog.php
to
http://new.kn3rdmeister.com/category/blog/ (and it works backwards too)
In the .htaccess:
RewriteEngine On
RewriteRule ^blog\.php$ /category/blog/ [R=301,L]
RewriteRule ^category/blog/?$ blog.php [L]
...
That works just dandy. Now what I would like to do is rewrite
/blog.php?pagenum=x
to
/category/blog/page/x/
In the .htaccess:
...
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD)\ blog\.php\?pagenum=([0-9]*)\ HTTP/
RewriteRule ^blog\.php$ /category/blog/page/%2/ [R=301,L]
RewriteRule ^category/blog/page/([0-9]+)/$ blog.php [L]
All I can get to work is the first rule, which ends up working, but it displays the QueryStrings rather than the oh-so-clean page directories, like so...
/blog.php?pagenum=2 for example equals
/category/blog/?pagenum=2
I hope I've made my question clear enough :/ If you need anything clarified, please ask and I'll update my post. Thanks in advance.
Add a question mark to the end of your target in your RewriteRule:
RewriteRule ^blog\.php$ /category/blog/page/%2/? [R=301,L]
My current .htacess looks like this:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9-z\-]+)$ index.php?page=$1
RewriteRule ^([a-zA-Z0-9-z\-]+)/$ index.php?page=$1
which allows me to take a a link such as
http://www/myserver.com/somepage
and rewrite it into
http://www.myserver.com/?page=somepage.
but how would i make it go deeper? I want to be able to do:
http://www.myserver.com/somepage/subpage
and turn it into
http://www.myserver.com/?page=somepage&subpage=subpage
Thanks
Add the following rules with additional capturing group for the second parameter
RewriteRule ^([a-zA-Z0-9-z\-]+)/([a-zA-Z0-9-z\-]+)$ index.php?page=$1&subpage=$2
RewriteRule ^([a-zA-Z0-9-z\-]+)/([a-zA-Z0-9-z\-]+)/$ index.php?page=$1&subpage=$2
If you have the potential to catch links that do not have subpages, then you'd want to catch the subpages with the first rule. Then you can catch the rest and send to page.
RewriteEngine On
# catch potential subpages first
RewriteRule ^([a-zA-Z0-9-z\-]+)/([a-zA-Z0-9-z\-]+)/?$ index.php?page=$1&subpage=$2 [L]
#combines your original rule to allow for optional end slash
RewriteRule ^([a-zA-Z0-9-z\-]+)/?$ index.php?page=$1 [L]
You could combine them into one rule, but it would potentially generate queries with an empty subpage. I prefer the cleaner internal queries, but for arguments sake:
RewriteEngine On
# One rule to rule them all
RewriteRule ^([a-zA-Z0-9-z\-]+)(/([a-zA-Z0-9-z\-]+))?/?$ index.php?page=$1&subpage=$3 [L]
Edit: Throwing in this link to a rewrite tester. Very useful. Although, when I tested my rules, it brought up an issue with your original [] grouping. What is the -z- at the end for?
RewriteEngine On
RewriteRule ^([a-zA-Z0-9-z\-\_]+)$ index.php?page=$1
RewriteRule ^([a-zA-Z0-9-z\-\_]+)/$ index.php?page=$1
RewriteRule ^([a-zA-Z0-9-z\-\_]+)/([a-zA-Z0-9-z\-\_]+)$ index.php?page=$1&id=$2
RewriteRule ^([a-zA-Z0-9-z\-\_]+)/([a-zA-Z0-9-z\-\_]+)/$ index.php?page=$1&id=$2
RewriteRule ^([a-zA-Z0-9-z\-\_]+)/([a-zA-Z0-9-z\-\_]+)/([a-zA-Z0-9-z\-\_]+)$ index.php?page=$1&id=$2&value=$3
RewriteRule ^([a-zA-Z0-9-z\-\_]+)/([a-zA-Z0-9-z\-\_]+)/([a-zA-Z0-9-z\-\_]+)/$ index.php?page=$1&id=$2&value=$3