The title is quite ambiguous so I apologise.
The problem lies in the following code. I need to add actions after the keyword statement however I do not want the action to override the keyword when action is not present.
The input url with actions is http://websitenamehere/uk/en/meet-us/update
and without: http://websitenamehere/uk/en/meet-us
So I want the actions clause to be triggered only when what proceeds it is a string of 4 or more characters. This htaccess rule should not depend on the country or lang being set when making the comparison.
If I have missed anything please let me know and I will update the question. I know it should be fairly simple but at the moment I am just blundering through htaccess and leanring as I go.
#language
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-z]{2})/([a-z]{2})(?:/(.*)|)$ /$1/$3?lang=$2 [NC,QSA,PT]
#country
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-z]{2})(?:/(.*)|)$ /$2?country=$1 [NC,QSA,PT]
#keyword
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/(.*)/$ /$2?keyword=$1 [NC,QSA,PT]
#actions
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php !-f
RewriteRule ^([^/]+)/(.*)$ /?action=$1 [NC,QSA,PT]
#withoutactions
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php !-f
RewriteRule ^([^/]+)/(.*)$ /$2?keyword=$1 [NC,QSA,PT]
RewriteCond %{REQUEST_URI} !(\.[^./]+)$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)/$ /$1.php [NC,QSA,PT]
I figured this out today. Basically what is happening is the htaccess file when building a back reference goes through the rules more than once.
I did add a skip into the original keyword line but it would simply be ignored:
#keyword
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/(.*)/$ /$2?keyword=$1 [NC,QSA,PT,S=1]
#withoutactions
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php !-f
RewriteRule ^([^/]+)/(.*)$ /$2?keyword=$1 [NC,QSA,PT]
#actions
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php !-f
RewriteRule ^([^/]+)/(.*)$ /?action=$1 [NC,QSA,PT]
When the htaccess ran the second time the S would no longer apply and it would then hit the withoutactions block. To circumvent this I simply checked if keyword already existed in the query string and if so to skip the rule:
#keyword
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/(.*)/$ /$2?keyword=$1 [NC,QSA,PT,S=1]
#withoutactions
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php !-f
RewriteCond %{QUERY_STRING} !keyword
RewriteRule ^([^/]+)/(.*)$ /$2?keyword=$1 [NC,QSA,PT]
#actions
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php !-f
RewriteRule ^([^/]+)/(.*)$ /?action=$1 [NC,QSA,PT]
I hope this helps someone experiencing a similar problem with back references!
Related
I'm trying to do some url like that: domain.com/category-subcategory.
If I go to domain.com/category Its work, but if I trying to go to domain.com/category-subcategory it is not working..
this is my code:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ category.php?category=$1 [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)-([^/]+)/$ category.php?category=$1&subcategory=$2 [L,NC]
How can I fix it? Thanks
Edit: I fixed it! here is the code:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)-([^/]+)$ category.php?category=$1&subcategory=$2 [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ category.php?category=$1 [L,NC]
I was only need to remove the slash before the $ at the end. Thanks for everyone!
Try with below,
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)-([^/]+)$ category.php?category=$1&subcategory=$2 [L,NC]
I need hide only one variable from get request my url:
http://example.com/profile/?id=5&username=demo
I trying to make:
http://example.com/player/demo
htaccess:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^player/([^/]*)$ /profile/?id=$1&username=$2 [QSA,B]
My solution here is to change
http://example.com/profile/?id=5&username=demo
to
http://example.com/player/5/demo
because if you use what you suggested
http://example.com/player/demo
there is an indication to username which is demo but there is no indication to id unless you do it like following :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^player/(.*)/(.*)$ /profile/?id=$1&username=$2 [QSA,L]
Here is the code I use to get three virtual subfolders with .htaccess:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ index.php?url=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ index.php?url=$1&sub=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ index.php?url=$1&sub=$2&subsub=$3 [L]
the question is: is it possible to make it simpler? For example:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ index.php?url=$1&sub=$2&subsub=$3 [L]
I know this won't work. I am just trying to figure out if it is the only way to add more subfolders. What if I need, let's say, six (url/sub/sub2/sub3/sub4/sub5....etc)??
Thank you in advance
I can't fix this issue. I'm trying to get rewrite rules to work with dynamic urls like:
http://example.com/[CATEGORY]/[PAGE]/[OPTIONS]
or
http://example.com/[PAGE]
This is my .htaccess
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} ^(.*[^/])
RewriteRule ^(.*[^/]) /index.php?url=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} ^(.*[^/])\/^(.*[^/])
RewriteRule ^(.*[^/])\/^(.*[^/]) /index.php?url=$1/$2 [QSA,L]
The first rewriterule is working, but the second isn't. Also is this not clean.
Anyone who knows this?
I have a rewriterule on my site thats like this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ open.php?url=$1 [L]
(There is a lot above it but this is the last rule)
It works very nicely.
What I want to achieve, however, is that if the url is as follows:
http://site.com/first/second
Then I want to redirect to
open.php?url=$1&open=$2
The problem is that I want to have both of them at the same time so that site.com/foo works as well as site.com/foo/bar . I also want to avoid having a trailing slash there all the time ergo both of the rules at the same time.
If there a way to check for trailing slashes with a rewritecond of some sort
any help is appreciated :)
SOLVED
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(.*)/(.*)$
RewriteRule ^(.*)$ open.php?url=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /(.*)/(.*)
RewriteRule ^(.*)/(.*)$ open.php?url=$1&open=$2 [L]
All I had to add was a RewriteCond %{REQUEST_URI} !^/(.*)/(.*)$ on the first rule!