How to achieve optional URL components with mod_rewrite RewriteRule? - .htaccess

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

Related

Trying to redirect from domain.com/user/12345 to domain.com/user?id=12345

I'm trying to redirect from domain.com/user/12345 to domain.com/user?id=12345 and from domain.com/user/12345/profile to domain.com/user/profile?id=12345.
I've written this rule in .htaccess:
RewriteRule ^user/([0-9]*)(/.*)?$ user$2?id=$1 [L,QSA]
It works ok for domain.com/user/12345/profile but domain.com/user/12345 is not redirected.
I've also tried with this simplest form for this case:
RewriteRule ^user/([0-9]*)$ user?id=$1 [L,QSA]
I've tested both in http://htaccess.madewithlove.be/ and seems to work fine.
¿What is my mistake? ¿How can I do it?
UPDATE:
The next rule I've in this file rewrites from domain.com/user to domain.com/user.php and so on. If the previous rule is not defined it rewrites ok from domain.com/user/12345 to domain.com/user/12345.php but with the previous rule defined it neither does this rewriting.
Does it means that there is any kind or transformation that skip the second rule or that there is any kind of misyake that stops rules verification?
I've found the solution. I don't know why it worked well in a case and not in the other but I'd forgotten the slash so it was trying to rewrite to user?id=12345and not to /user?id=12345.
So the rule must be like this one:
RewriteRule ^user/([0-9]*)(/.*)?$ /user$2?id=$1 [L,QSA]

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]

Url rewriting with parameters

I’m a noob in url rewriting and i really need help to figure this out :
I have this url : section/news.php?url=section/news/27-how_to_make_rewrite.html I want to access this news with the url parameter and the link would be : section/news/27-how_to_make_rewrite.html
the .htacess file is in the root, i have a folder named section and inside this folder i have the news folder
This doest work for me.
I have :
RewriteEngine on
RewriteBase /section/
RewriteRule news/^([a-zA-Z0-9-+/]+)/$ news/news.php?url=$1
How I can do it ?
I have not used this with subfolders, but I would try something like this.
RewriteRule ^news/([^/\.]+)/?$ news/news.php?url=section/news/$1 [L]
Edit
Try it this way:
RewriteRule ^section/news/([^/.]+)/?$ news.php?url=section/news/$1 [L]
You normally only want to put what is changing in the variable. section/news/ is always used so you should keep it outside of the replacement.
If the URI you want to rewrite to looks like: section/news.php?url=section/news/27-how_to_make_rewrite.html
then you've got one too many "news" in your target. You also need to fix your regex pattern to match stuff like 27-how_to_make_rewrite.html.
You want something like:
RewriteEngine on
RewriteBase /section/
RewriteRule news/(.+)/?$ news/news.php?url=section/news/$1

rewriteRule for URL path not working correctly

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.

.htaccess RewriteRule help

I have been able to change the 'location' of my images with RewriteRule, but I have 3 lines of code to do this because of subdirectories in the image folders
RewriteRule ^images/([^/]+).(jpg|jpeg|bmp|gif|png)$ /includes/images/$1.$2
RewriteRule ^images/([^/]+)/([^/]+).(jpg|jpeg|bmp|gif|png)$ /includes/images/$1/$2.$3
RewriteRule ^images/([^/]+)/([^/]+)/([^/]+).(jpg|jpeg|bmp|gif|png)$ /includes/images/$1/$2/$3.$4
However, I would like to be able to not need to add to these if I ever add any deeper subdirectories.
I have tried many different approaches to this with no luck, and the following is what I am stuck with atm.
RewriteRule ^(images)(/?[^/])+(.jpg|.jpeg|.bmp|.gif|.png)$ /includes/$&
Does anyone have any ideas on how to get this code to work??
Also, is there any way to view the URL that is being used server-side?
Perhaps I'm missing something, but I don't see any reason that you couldn't simply do:
RewriteRule ^images/(.+)\.(jpg|jpeg|bmp|gif|png)$ /includes/images/$1.$2

Resources