Add a subdirectory redirect - .htaccess

I am trying to add a new path level to my search, for example:
wwww.website.com/s/blablabla/2
becomes
www.website.com/s/videos/blablabla/2
2 being the page.
However, I can't get this to work with htaccess because it keeps looping (I think that since /s/videos/ contains /s/ it keeps doing the rewrite...)
So far, I tried these codes:
RewriteCond %{REQUEST_URI} !^/s/.*$
RewriteRule ^(.*)$ /s/videos/$1 [L]
This give me the following error:
The requested URL /s/videos/index.php was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
This code:
RewriteCond %{REQUEST_URI} ^/s/.*$
RewriteRule ^(.*)$ /s/videos/$1 [L]
Gives me the following error:
Internal Server Error
The server encountered an internal error or misconfiguration and was
unable to complete your request.
Please contact the server administrator,
webmaster#website.com and inform them of the time the error
occurred, and anything you might have done that may have caused the
error.
More information about this error may be available in the server error
log.
I also tried
RewriteRule !^s(/|$) s/videos%{REQUEST_URI} [L]
and
RewriteRule ^s/(.*)$ /s/videos/$1 [R=301,NC,L]
But none seems to work...
EDIT:
RewriteRule ^s/([A-Za-z0-9-\s]+)/?$ /s/videos/$1 [R=301,NC,L]
Is working but only if there is no page after the search tag...
EDIT2:
I was able to get it working by adding multiple lines for the different scenarios:
RewriteRule ^s/([A-Za-z0-9-\s]+)/?$ /s/videos/$1 [R=301,NC,L]
RewriteRule ^s/([A-Za-z0-9-\s]+)/([0-9]+)/?$ /s/videos/$1/$2 [R=301,NC,L]
Not sure if it's the proper way to do it at all though
if I do
RewriteRule ^s/(.*)$ /s/videos/$1 [R=301,NC,L]
It gives me
/s/videos/videos/videos/videos/videos/videos/videos/videos/videos/videos/videos/videos/videos/videos/videos/videos/videos/videos/videos/videos/videos/searchtag
EDIT3:
RewriteCond %{REQUEST_URI} !^/s/(videos|members|photos)
RewriteRule ^s/(.*)$ /s/videos/$1 [R=301,NC,L]
This is working...

Try this code.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^s/([^/]+)/([0-9]+)/?$ /s/videos/$1/$2 [L]

Related

Rewrite url with query parameter

I would like to rewrite a specific url and remove the query parameter. However online I can't seem to find a solution which works for me. I'm pretty new to .htacces files and the working of them.
My file for now, which is in the main folder of my website:
ErrorDocument 404 /404.html
#Remove php extension
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
#Remove html extension
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
#Remove id query parameter
RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^webhook/(.*)$ webhook.php?q=$1
So the normal url is: https://example.com/api/webhook.php?id=10
which I want rewrite as: https://example.com/api/webhook/10
Does anyone know how to create this for this specific case? Thanks in advance!
EDIT:
In my webhook.php file I have this for testing purposes:
<?php echo $_GET['id']; ?>
When I go to the url https://example.com/api/webhook.php?id=10 I get output 10 in the browser.
When I go to the url https://example.com/api/webhook/10
I get this error message:
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
And in the serverlogs I found this error:
Request exceeded the limit of 4 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace
Seems there are too many rewrites.
SOLVED
I found that these lines fixed my problem:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^api/webhook/(.+)$ api/webhook.php?id=$1 [L]
Now when I go to https://examply.com/api/webhook/10 it loads the webhook.php script with query ?id=10. The page now outputs '10'.
RewriteEngine On
RewriteRule ^api/webhook/([0-9]+)/?$ api/webhook.php?id=$1 [NC,L,QSA]
Usage = These rules will make your url https://example.com/api/webhook.php?id=10 to https://example.com/api/webhook/10

Rewrite subdomain to subfolder causes loop error or 403

I have a local Apache server set up on my machine, with wildcard DNS in place. I have it set up so that it works like [foldername].loc. So, for instance, a folder under my htdocs folder called MyDomain, would be accessed via mydomain.loc. This code works fine, and the code in my .htaccess in my htdocs is below:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]*\.)?([a-z0-9-]+)\.loc$ [NC]
RewriteCond %3::%{REQUEST_URI} !^(.*?)::/\1/?
RewriteRule (.*) /%3/$1 [PT,QSA]
Now, the above code also passes through subdomains, such as "john.mydomain.loc". Now, I have the following folder structure in the folder MyDomain:
MyDomain
- active
- index.php
- working
- index.php
.htaccess
In the .htaccess of MyDomain is the following code:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^live\.mydomain\.loc$ [NC]
RewriteRule ^(.*)$ /active/$1 [L]
What this should do, if I understand correctly, is take http://live.mydomain.loc/ and rewrite it to be http://mydomain.loc/active/. Note that I said rewrite, not redirect.
With the code above, however, I get a message in the Apache error log:
[client 127.0.0.1] Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.
If I change the .htaccess of MyDomain to read as follows:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^live\.mydomain\.loc$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /active/$1 [L]
When I use this code, it always comes up with a 403 error, saying I don't have permission to view the folder /mydomain/. If I set Options +Indexes, I only see the folder index of /mydomain. So where along the line is the above code failing?
I have also tried the above code with RewriteCond %{REQUEST_URI} !^/active/. This has made no difference in the results.
I have tried this for over two days, and I can't figure it out. I hope the brilliant minds of StackOverflow can help figure this out. :)
Try setting the rewrite base to where the file actually is:
RewriteEngine On
RewriteBase /MyDomain/
RewriteCond %{HTTP_HOST} ^live\.mydomain\.loc$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ active/$1 [L]
And making the /active/ relative: active/
I solved this using the code below:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^live\.mydomain\.loc$ [NC]
RewriteCond %{REQUEST_URI} !^/mydomain/active/
RewriteRule ^(.*)$ active/$1 [L]
The primary fix was RewriteCond %{REQUEST_URI} !^/mydomain/active/. The difference is, the first line I tried, RewriteCond %{REQUEST_URI} !^/active, would match /access/index.php, but not /mydomain/active/index.php.
I hope this helps someone else.

.htaccess RewriteRule not working with querystrings as directory

I have the following code:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
RewriteRule ^(.*)$ $1/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)/$ $1.php
RewriteRule ^([^\.]+)/([^\.]+)/$ $1.php?id=$2
I had problems with with the absolute URI, it solved. Now I want to use the last row for the following:
domain.com/query/ping/2/
to
domain.com/query/ping.php?id=2
I think the code is good, but I still get back error 404. Should I give some rewrite conditions? I searched for this code but haven't found anything useful.
Based on where you've placed your htaccess file in your previous question, you probably want something like this:
RewriteCond %{REQUEST_URI} ^/query/([^/]+)/([^/]+)/?
RewriteCond %{DOCUMENT_ROOT}/query/%1.php -f
RewriteRule ^ /query/%1.php?id=%2 [L]
The important issue is that you need to check for /query in the beginning of the URI. What you have, ^([^\.]+)/([^\.]+)/$ won't match the /query part along with the 2 pathnames after it. Your regex only matches 2 pathnames.
The line RewriteCond %{DOCUMENT_ROOT}/query/%1.php -f is similar to the condition you have above where it checks to see if the requested PHP file actually exists, otherwise it won't blindly rewrite. This condition ensures if someone tries to go to:
http://domain.com/query/blahblahblahblah/blah
Your server won't return a 404 error saying /query/blahblahblahblah.php doesn't exist.

Redirect only certain requests to controller file using RewriteRule

I've created a controller file which handles redirecting users to the proper url, I want to direct certain traffic to this controller and not affect the rest. http://website.com/shows.php?id=review-1 is an example of the url I want to send to the controller.
What I've concocted thus far is the following RewriteRule which causes a site-wide server 500 Server Error.
RewriteCond %{THE_REQUEST} ^GET\s/shows\.php\?
RewriteCond %{QUERY_STRING} (?:^|&)id=review-[0-9]+(?:&|$) [NC]
RewriteRule ^ /controller.php [QSA,L]
Removing the -[0-9]+ from the %{QUERY_STRING} condition gets rid of the site-wide 500 Server Error, but doesn't work. The following RewriteRule that I'm trying to replace works just fine.
RewriteCond %{THE_REQUEST} ^GET\s/shows\.php\?
RewriteCond %{QUERY_STRING} (?:^|&)id=review-1(?:&|$) [NC]
RewriteRule ^ /review/1/super-baseball-2020/? [R=301,L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z0-9]+)/([0-9]+)/([a-z0-9-]+)?/?$ /shows.php?id=$1-$2 [QSA,L,NC]
The end result is that a user who goes to http://www.website.com/shows.php?id=review-1 will end up at http://www.website.com/controller.php?id=review-1 which will deal with handling the 301 redirect.
I still don't know why you're trying to make simple things into complex ones ;)
6 minutes to find+test. Here's what should work:
RewriteCond %{QUERY_STRING} (^|(.)+&)(id=review-([0-9]+))($|&.*) [NC]
RewriteRule /shows.php /controller.php?%3 [QSA,L]
PS: Like Devin, I don't get/know why (?:^|&) works...
Interesting. Doesn't' look wrong, but I couldn't get the line
RewriteCond %{QUERY_STRING} (?:^|&)id=review-[0-9]+(?:&|$) [NC]
to compile in mod_rewrite until I took out the non-capturing indicators
RewriteCond %{QUERY_STRING} (^|&)id=review-[0-9]+(&|$) [NC]
seems to compile ok on my system, does that work for you?
I think you need to ascertain what's causing the 500 error. I suspect the redirect is working, but the page you're being redirected to is failing.

Change htaccess path?

This code works. What it does is rewrite's a subdomain so a specific directory will display. Basically it enables wildcard domains to exist.
Currently it does this.
bob.domain.com goes to public_html/-bob
I would like it to go to public_html/__sites/-bob. I tried the code below at the bottom but didn't have any joy. Help would be appreciated.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/-
RewriteCond %{HTTP_HOST} ^([^\./]+)
RewriteCond %{DOCUMENT_ROOT}/-%1 -d
RewriteRule ^(.*)$ -%1/$1 [L]
However it doesn't do what I want it to do.
Instead of it redirecting to root/-directory
I want it to redirect to root/__sites/-directory
I have tried this but I get a server error
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/-
RewriteCond %{HTTP_HOST} ^([^\./]+)
RewriteCond %{DOCUMENT_ROOT}/__sites/-%1 -d
RewriteRule ^(.*)$ __sites/-%1/$1 [L]
How do I tweak this code to get it working?
As I understand you having 500 Internal Server Error. If you check your error log, there will be detailed explanation on the reason that caused it. I'm pretty sure it will be "Number of iterations exceeded" or something like that.
Considering my assumption is correct, this will be the correct rule:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/__sites/
RewriteCond %{HTTP_HOST} ^([^\./]+)
RewriteCond %{DOCUMENT_ROOT}/__sites/-%1 -d
RewriteRule ^(.*)$ __sites/-%1/$1 [L]
The error caused by the fact that rewrite is not stopped when you specify the [L] flag -- it only goes to next iteration .. where your current rule will be executed again .. and again.
You have updated destination path from public_html/-bob to public_html/__sites/-bob .. but forgot to update the very first condition: should be %{REQUEST_URI} !^/__sites/.

Resources