RewriteRule in same .htaccess on two different files - apache

I want to make a rewrite rule that will work on two different files which are contained in the same folder.
localhost/nameOfUser will be applied to index.php
localhost/nameOfUser?score=12 or localhost/nameOfUser/12 will be applied to post.php
I've managed to make the first one work with
RewriteEngine On
RewriteRule ^(css|fonts)($|/) - [L]
RewriteRule ^(php|avatars)($|/) - [L]
RewriteRule ^(user|post)($|/) - [L]
RewriteRule ^(.*)$ index.php?user=$1 [NC,QSA]
if I add
RewriteRule ^(.*)$ post.php?user=$1 [NC,QSA]
The second starts to work, but the first one doesn't.
Is it even possible to make rewrite for two different files in the same .htaccess file?

Give the following rules a try:
RewriteEngine On
RewriteRule ^(css|fonts|php|avatars|user|post)($|/) - [L]
RewriteRule ^([^/]+)$ index.php?user=$1 [NC,QSA,L]
RewriteRule ^([^/]+)/([^/]+)$ post.php?user=$1&score=$2 [NC,QSA,L]
However, if you want nameOfUser?score=12 to work, you'll need to modify them to:
RewriteEngine On
RewriteRule ^(css|fonts|php|avatars|user|post)($|/) - [L]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^([^/]+)$ /index.php?user=$1 [NC,QSA,L]
RewriteCond %{QUERY_STRING} ^score=(.+)$
RewriteRule ^([^/]+) /post.php?user=$1&score=%1 [NC,L]

Related

Rewrite partial URL to specific page with .htaccess mod_rewrite

Couldn't really figure out a title for this, so it's not the best, sorry.
I'm looking to rewrite URLs like this:
https://awesomechristianmusic.com/song-list?artist=dc-talk
To look like this:
https://awesomechristianmusic.com/dc-talk
Or for genres/topics:
https://awesomechristianmusic.com/song-list?genre=rock
https://awesomechristianmusic.com/song-list?topic=forgiveness
To look like this:
https://awesomechristianmusic.com/genre/rock
https://awesomechristianmusic.com/topic/forgiveness
Unfortunately, there seem to be some issues with my .htaccess file and I can't figure it out. Here's what I'm working with:
RewriteEngine on
RewriteBase /
# Force HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,QSA,R=301]
# Remove www.
RewriteCond %{HTTP_HOST} ^www\.(.*)$
RewriteRule ^(.*)$ https://%1/$1 [R=301,L,QSA]
# Remove trailing slashes
RewriteCond %{REQUEST_URI} ^.*/$
RewriteRule ^(.*)/$ https://%{HTTP_HOST}/$1 [R=301,L,QSA]
# This is where I'm currently working
RewriteRule ^/([^/]+)$ /song-list?artist=$1 [NC,L,QSA]
RewriteRule ^/genre/([^/]+)$ /song-list?genre=$1 [NC,L,QSA]
RewriteRule ^/topic/([^/]+)$ /song-list?topic=$1 [NC,L,QSA]
# Redirect everything through index.php
RewriteCond %{REQUEST_URI} !(/(api|edit)/?.*) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [NC,L,QSA]
# long list of 301 redirects
Currently, the three rewrites I'm working on don't do anything. If I remove the slashes at the beginning of their regexes, I get a 500 Internal Server Error. Any ideas what might be going wrong or how to fix it?
Swap out these three rules:
# This is where I'm currently working
RewriteRule ^/([^/]+)$ /song-list?artist=$1 [NC,L,QSA]
RewriteRule ^/genre/([^/]+)$ /song-list?genre=$1 [NC,L,QSA]
RewriteRule ^/topic/([^/]+)$ /song-list?topic=$1 [NC,L,QSA]
To these three rules:
# If not /genre/* and not /topic/*
# Convert /dc-talk to /song-list?artist=dc-talk
RewriteCond %{REQUEST_URI} !^/genre/(.*)$ [NC]
RewriteCond %{REQUEST_URI} !^/topic/(.*)$ [NC]
RewriteRule ^(.*)$ song-list?artist=$1 [NC,L,QSA]
# Convert /genre/rock to /song-list?genre=rock
RewriteRule ^genre/(.*)$ song-list?genre=$1 [NC,L,QSA]
# Convert /topic/forgivenessto /song-list?topic=forgiveness
RewriteRule ^topic/(.*)$ song-list?topic=$1 [NC,L,QSA]
You can view these rules in action here: https://htaccess.madewithlove.be/?share=763f7107-f056-5ebe-ae0a-7f53a17b225a

RewriteRule with question marks htaccess

i know this been asked a lot
but i still did not succeeded to do it
i have this in my htaccess
RewriteRule ^embed/([0-9A-Za-z]{12})$ /cgi-bin/index_dl.cgi?op=video_embed&file_code=$1 [L]
RewriteRule ^embed/([0-9A-Za-z]{12})/(\d+)x(\d+)$ /cgi-bin/index_dl.cgi?op=video_embed2&file_code=$1&w=$2&h=$3 [L]
i want it to be
RewriteRule ^embed/?v=([0-9A-Za-z]{12})$ /cgi-bin/index_dl.cgi?op=video_embed&file_code=$1 [L]
RewriteRule ^embed/?v=([0-9A-Za-z]{12})/(\d+)x(\d+)$ /cgi-bin/index_dl.cgi?op=video_embed2&file_code=$1&w=$2&h=$3 [L]
adding ?v=
RewriteCond %{QUERY_STRING} ^v=(.*)$ [NC]
RewriteRule ^embed/%1([0-9A-Za-z]{12})/(\d+)x(\d+)$ /cgi-bin/index_dl.cgi?op=video_embed2&file_code=$1&w=$2&h=$3 [NC,L]
RewriteRule ^embed/%1([0-9A-Za-z]{12})$ /cgi-bin/index_dl.cgi?op=video_embed&file_code=$1 [NC,L]
not working
example
mysite.com/embed/?v=abcde
mysite.com/embed/?v=abcde/6x6
currently it is
mysite.com/embed/abcde
mysite.com/embed/abcde/6x6
so now this work
RewriteCond %{QUERY_STRING} ^v=(.*)$ [NC]
RewriteRule ^embed/?$ /cgi-bin/index_dl.cgi?op=video_embed2&file_code=%1 [L]
i have noticed that i have this in the file
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule ^(.*) - [E=HTTP_CGI_AUTHORIZATION:%1]
RewriteCond %{QUERY_STRING} ^v=(.*)$ [NC]
RewriteRule ^$ /cgi-bin/index_dl.cgi?op=download1&id=%1 [NC,L]
i am still trying to make this to work
RewriteCond %{QUERY_STRING} ^v=(.*)/(\d+)x(\d+)$ [NC]
RewriteRule ^embed/?$ /cgi-bin/index_dl.cgi?op=video_embed2&file_code=%1&w=$2&h=$3 [L]
You are not using the rules properly - passing %1 to the rules won't help you, because you're not matching correctly. REQUEST_URI and QUERY_STRING are two different things. RewriteRule is only able to check the REQUEST_URI, and not the QUERY_STRING, as shown in the answer suggested by Panama Jack.
To achieve what you want, you need to use the following instead:
RewriteEngine On
# Check for mysite.com/embed/?v=abcde/6x6
RewriteCond %{QUERY_STRING} ^v=([^&/]+)\/(\d+)x(\d+)$ [NC]
RewriteRule ^embed/?$ /cgi-bin/index_dl.cgi?op=video_embed2&file_code=%1&w=%2&h=%3 [L]
# Check for mysite.com/embed/?v=abcde
RewriteCond %{QUERY_STRING} ^v=([^&]+)$ [NC]
RewriteRule ^embed/?$ /cgi-bin/index_dl.cgi?op=video_embed&file_code=%1 [L]
Here, we simply check the query string for each case. If there is a match, only rewrite if we're at /embed/ (trailing slash optional - you may remove the question mark if you wish for the trailing slash to be required).

.htaccess redirect for all files but with exceptions

I know this has been asked thousands times, but I've been googling for three hours without any result. So I'm asking here. I'm creating a website. All the content is in the folder /subfolder. Now I want to redirect all the requests to another domain (let's say domain.com), at the exception of the files that I actually use. It may seem weird but it makes sense in my situation. So what I have at the moment is
RewriteEngine on
RewriteRule ^account$ account.php
RewriteRule ^home$ myhome.php
RewriteRule ^options$ options.php
RewriteRule ^login$ login.php
RewriteRule ^links$ editlinks.php
RewriteRule ^help$ howto.php
RewriteRule ^$ index.php
RewriteRule ^forgot$ forgot.php
RewriteRule ^r$ redirect.php
RewriteRule ^r/(.*)$ redirect.php?id=$1
RewriteCond %{REQUEST_URI} !(login.php|login)
RewriteRule (.*) external.php?parameter=$1 [L]
How can I do this ? the above code always redirects me to domain.com.
Thank you in advance
Make sure to place redirect just below RewriteEngine On and use THE_REQUEST instead of REQUEST_URI like this:
DirectoryIndex index.php
RewriteEngine on
RewriteCond %{THE_REQUEST} !/media [NC]
RewriteCond %{THE_REQUEST} !/(login|signup)(\.php)?[?\s] [NC]
RewriteRule (.*) http://domain.com/$1 [R=301,L]
RewriteRule ^home$ myhome.php [L]
RewriteRule ^links$ editlinks.php [L]
RewriteRule ^help$ howto.php [L]
RewriteRule ^(account|login|options|forgot)/?$ $1.php [L,NC]
RewriteRule ^r$ redirect.php [L]
RewriteRule ^r/(.+)$ redirect.php?id=$1 [L,QSA]

this mod_rewrite rule has me for a loop

I want to have /path/short serve up the content held by the file /path/short.html, but if the user types /path/short.html or /path/short/ I want him to be redirected (with 301) to /path/short. This is what I have so far in my .htaccess:
RewriteEngine On
RewriteBase /path
RewriteCond %{REQUEST_URI} !=/path/handle.html
RewriteCond %{REQUEST_URI} !^/path/short(.html|/)?$
RewriteRule ^(.+) /cgi-bin/handle\.cgi?$1 [PT,QSA]
RewriteRule ^path$ /path/short.html
#-------
RewriteRule ^short/$ /path/short [R=301,L]
# RewriteRule ^short.html$ /path/short [R=301,L]
The rules reflect the fact that handle.cgi will receive all other requests (ie, /path/this, /path/, /path/somethingelse, etc.) which is working great. So far, the /path/short/ redirects correctly to /path/short and /path/short is properly showing the content in /path/short.html, however, uncommenting the last line causes a loop. So, how do I get this to work?
You can use this code in /path/.htaccess:
RewriteEngine On
RewriteBase /path/
RewriteCond %{THE_REQUEST} /(short)(/|\.html) [NC]
RewriteRule ^ %1? [R=301,L]
RewriteRule ^(short)/?$ $1.html [L,NC]
A friend pointed me towards another question which helped me arrive at this solution:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/ads/short(.html|/)?$
RewriteRule ^(.+) /cgi-bin/handle\.cgi?$1 [PT,QSA]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^short(.html|/)$ /ads/short [R=301]
RewriteRule ^short$ /ads/short.html
which works perfectly.

.htaccess Rewrite GET-Data into GET-Data

i want to rewrite http://192.168.2.101/game.php?village=567919&screen=overview into http://192.168.2.101/index.php?dir=game&page=overview&village=567919 can anyone tell my why this is not working well and how i do it right?
my .htaccess code
RewriteEngine On
RewriteRule ^$ index/
RewriteRule ^(.*)/$ index.php?dir=main&page=$1 [L,QSA]
RewriteRule ^game.php?village=(.*)&screen=(.*)$ index.php?dir=game&page=$2&village=$1 [L,QSA]
You have to use %{QUERY_STRING} to parse query string parameters.
Replace your code by this one in your htaccess
RewriteEngine on
RewriteCond %{QUERY_STRING} ^village=([^&]+)&screen=([^&]+)$
RewriteRule ^game\.php$ /index.php?dir=game&page=%2&village=%1 [L]
RewriteRule ^(.+)/$ /index.php?dir=main&page=$1 [L]
RewriteRule ^$ /index.php [L]