rewriteRule for URL path not working correctly - .htaccess

I need help with a URL problem I've encountered with a rewriteRule.
What I need it to do is following: example.com/en/page/page/
At the moment the following works fine: example.com/en/page/
But once it goes like "example.com/en/page/page/" I receive a 404 - page not found error even if the page in fact is located in the serverfiles.
The clue here is that I use a variable in the /en/ part of the URL (multilanguage system) and it seems that I cannot figure out how to get it to work with that included.
At the moment I have the following rewriteRule in my .htaccess file.
RewriteRule ^([^/]*)/([^/]*)/?$ index.php?lang=$1&uri=$2 [L]
Do any of you have a clue on what might work?
Best regards,
PureDarkness

You don't include anything behind the second /. You could try:
RewriteRule ^([^/]*)/(.*)$ index.php?lang=$1&uri=$2 [L]
And you can add [QSA] if you also need to get the parameters.

Related

Redirects not working as expected

I have an .htaccess file with several lines. It does not work as expected. Mod_rewrite is enabled. RewriteLogLevel is set to 9.
The first two rules are there to forbid uris with a length more then 80 characters:
RewriteCond %{REQUEST_URI} ^.{80}
RewriteRule .* - [F]
It does not seem to get evaluated as every test url passes through and it does not generate an error either.
I also tried:
RewriteRule .{80} - [F]
But that did not do the trick either. The process ends with a 404, not a 403.
This next rule is not working either. It used to work.
RewriteRule ^(\/)?([\w]+)$ /index.php [L]
The URI /Contact was always handled by this index.php.
Whatever URL I type I get a 404. I should get a 403 or a 200. Not a 404. What am I missing?
Apache has on all directories the permission to read, write and execute and on all files the permission to read and write.
The two urls for testing are:
127.0.0.4/asssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssddddddddddddddddddddd?p=s&s=psv
and
127.0.0.4/Contact
The alias for 127.0.0.4 used is considerate.lb.
Try this rule instead:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+\S{80}
RewriteRule ^ - [F]
Using THE_REQUEST instead of REQUEST_URI as that variable might get overwritten due to presence of other rules in your .htaccess
Finally I have found a solution. The problem was not in the coding of the .htaccess. I replaced the file with a previous version, added the new lines to test the request and it worked all fine.
It is not a satisfactory solution, because it can happen again and I do not have any clue what caused the error. If someone knows the error, I would love to hear what might have been the exact cause and how to solve that properly. I would like to change the tags of the question as the current tags might be misleading (although other people might experience the same problem how apache handles a .htaccess file), but I do not know which tags I should use.

How to remove .php from a certain file (with Apache .htaccess)?

I want to remove the php extension for a certain file (only this!). BUT: It uses get parameters and they should remain!
I tried it with my .htaccess and something like: RewriteEngine on followed by either
RewriteRule ^file(.*)$ file.php$1
or
RewriteRule ^file(.+)$ file.php$1
But it doesn't work. (First gives error 500, second gives 404)
Example what I want to call in the browser:
file?param=asd&foo=bar
It should call as:
file.php?param=asd&foo=bar
I found the solution myself. Maybe anyone wants to know:
RewriteRule ^file$ file.php [QSA]

Trying to exclude css and js files with IIRF rewrite

I'm trying to get IIRF working so that it rewrites certain URLs, but ignores files like .css and .js files.
I have URLs like:
/admin/
/admin/user/
/admin/user/view-details/1
/admin/user/view-details/1?edit
Which all work, but I can't seem to get this to work:
/admin/_assets/css/admin.css
My .ini file looks like this:
RewriteRule /admin/([^/]+)/([^/]+)/([^/]+)\?edit$ /admin/index.php?edit&action=$1&sub-action=$2&id=$3 [L,I,QSA]
RewriteRule /admin/([^/]+)/([^/]+)/([^/]+)$ /admin/index.php?edit&action=$1&sub-action=$2&id=$3 [L,I,QSA]
RewriteRule /admin/([^/]+)/$ /admin/index.php?action=$1 [L,I,QSA]
RewriteRule ^/admin/(.*)$ - [L]
I've also tried:
RewriteRule (.+\.)(php|jpg|png|jpeg|gif|ttf|sql|txt|htm|zip|css)$ - [L]
The things I've tried give either a blank document when I navigate directly to the css file, or I get:
HTTP Error 404.4 - Not Found
The resource you are looking for does not have a handler associated with it.
I don't have much experience with IIRF or IIS, so I'm a bit stuck with what to try next. Am I looking in the wrong place? Or have I made a really basic mistake?
Thanks
Tip for debugging iirf: use StatusUrl to check first for problems.
I've managed to fix this by adding:
RewriteRule /admin/_assets/(.+)$ /admin/_assets/$1 [L,I,QSA]
to the top of my file. I'm still not sure why nothing is being logged, but this fixed it for my needs.

RewriteRule not working

This is the contents of my .htaccess file:
RewriteEngine on
RewriteRule ^upload$ upload.php
RewriteRule ^/(\d+)/?.*$ /view.php?id=$1 [L]
The first rule successfully works. When I navigate to http://localhost/upload it shows the upload.php page.
The second rule however, does not. When I browse to: http://localhost/1234/some-string I get a 404 error. It's meant to show this page: http://localhost/view.php?id=1234.
Hopefully you can see what I'm trying to do with the rule, I want the last string on the end of the URL to be completely ignored, and take the 1234 as a parameter for view.php.
Can anyone spot why this isn't working? I've tried everything I can think of, but to no success. Thanks!
it will be trying to find the directory /1234/ and failing. change the / to a - and it should work
EDIT: got that completely wrong ... it's actually that you have a / at the beginning of your pattern, whereas MOD_REWRITE receives the path without the first slash.

How to achieve optional URL components with mod_rewrite RewriteRule?

I have set.php?mod=avatar set.php?mod=info. Now I want make it set/mod this, using this:
RewriteRule ^set/(.*)/?$ set.php?mod=$1
It's working, but when open it like set/, it's give me 404 error. How can I make it in one pharse?
Like:
RewriteRule ^set/if this exist ok if not I want it equal set.php only(.*)/?$ set.php?mod=$1
What about:
RewriteRule ^set/([^/]+)/?$ set.php?mod=$1

Resources