htaccess internal rewrite for URL with query params - apache

I have a php file /var/www/somepath/index.php which handles requests to
http://example.com/somepath/?q=query
I'd like the same file to handle requests to
http://example.com/some/other/path/?q=query
http://example.com/another/path/?q=query
http://example.com/yet/another/path/?q=query
I do not want to copy the php file into multiple locations in the filesystem. I thought using internal rewrite rules would be a better way to do do it. Here is what I put in top-level .htaccess file /var/www/.htaccess:
RewriteRule ^some/other/path /somepath/%{QUERY_STRING} [PT]
But it does not work. I get a 404 for http://example.com/some/other/path/?q=query
I know that mod_rewrite is enabled because I have external rewrite rules (301 and 302 redirects) in the same .htaccess files and those work.

You can place this rule as your very first rule in the site root .htaccess which will handle any URL with ?q= query parameter:
RewriteEngine On
RewriteCond %{QUERY_STRING} (^|&)q= [NC]
RewriteRule ^ somepath/index.php [L]

Related

how do I rewrite a URL and maintain the file name

I have a rewrite written in my .htaccess file. I am trying to redirect the following
https://olddomain.com/folder/file.pdf to https://newdomain.com/folder/file.pdf. file.pdf can change so I need to change the domain but leave the folder and file name needs to stay what ever it is. it could be file.pdf or file1.pdf etc
I have this code in my .htaccess file
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/folder/(.*)$
RewriteRule ^(.*) https://newdomain.com/folder/%1 [R=301,NC]
If the file.pdf exists on the old server then the redirect works but if the file does not exist on the old server the redirect does not work.
Any help fixing this would be appreciated.
If the file.pdf exists on the old server then the redirect works but if the file does not exist on the old server the redirect does not work.
That sounds like you've put the rule/redirect in the wrong place. If you have other directives before this redirect that implement a front-controller pattern then you will experience this same behaviour since any request for a non-existent file would be routed to the front-controller (and request for an existing file is ignored) before your redirect is triggered - so no redirect occurs.
If this is the case then you need to move your rule to the top of the file, before any existing rewrites.
RewriteCond %{REQUEST_URI} ^/folder/(.*)$
RewriteRule ^(.*) https://newdomain.com/folder/%1 [R=301,NC]
However, your existing rule is not quite correct. Importantly, you are missing the L flag on the RewriteRule directive and the preceding RewriteCond directive is not required. For example, try the following instead:
RewriteRule ^folder/.* https://newdomain.com/$0 [NC,R=301,L]
This does assume your .htaccess file is located in the document root of the site.
Alternatively, you create an additional .htaccess file inside the /folder with the following:
RewriteEngine On
RewriteRule ^ https://newdomain.com%{REQUEST_URI} [R=301,L]
The REQUEST_URI server variable contains the full URL-path of the request (including the slash prefix).
By default, the mod_rewrite directives in the /folder/.htaccess file will completely override any directives in the parent (root) .htaccess file (the mod_rewrite directives in the parent are not even processed).

Rewrite request in subdirectory after rewriting into that subdirectory

I have following file structure in my server:
/
source/ # source PHP files (not accessible)
public/ # publicly accessible files (media)
.htaccess # second level htaccess to rewrite path into variable
index.php # request collector
.htaccess # first level htaccess to redirect flow into public dir
In first level htaccess I have rewrite rule moves flow into "public" directory.
RewriteRule ^(?!public/).*$ public/$0 [L,NC]
"public/.htaccess" contains following code:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{QUERY_STRING} !index.php
RewriteRule ^(.*)$ index.php/$1 [QSA,L]
So if requesting existing file it is served. Otherwise request is rewrited like "example.com/namespace/controller/action?var=blah" to "example.com/index.php/namespace/controller/action?var=blah"
And that request should be handled by "public/index.php". However it seems that multiple rewriting is not allowed. Am I right? Or maybe I am making something wrong. I have to admit I can not understand mod_rewrite.

CodeIgniter on subdomain and htaccess

I'm trying to setup a website based on CodeIgniter.
For developing I want to have the domain dev.XXX.com
I changed my structure so I've got a no_www and a public_www folder.
Both are in the root of dev.XXX.com
So my idea was to rewrite url's form
dev.XXX.com/index.php/test
to
dev.XXX.com/public_www/index.php/test
So I just want add public_www to all the requests
In my htaccess file I've got:
RewriteRule ^(.*)$ /public_www/$1 [L]
But I always get 500 - Internal Server Error
Try adding the following to the .htaccess file in the root directory (public_www) of your site.
RewriteEngine on
RewriteBase /
#if its on dev.xxx.com
RewriteCond %{HTTP_HOST} ^dev\.XXX\.com$ [NC]
#if its not already public_www/ rewrite to public_www/
RewriteRule ^(?!public_www/)(.*)$ /public_www/$1 [L,NC]
The rule you had would lead to an infinite rewrite and subsequent 500 error. The one above should prevent that.
EDIT
could you maybe explain why my version leads into a infinite loop
I am likely incorrect about the infinite loop, but below is what will happen
Start with any input
^(.*)$ pattern will match any input
It will rewrite to /public_www/$1
.htaccess rules will be run again
^(.*)$ pattern will match rewritten input /public_www/$1
It will rewrite to /public_www/public_www/$1
At this point it will likely fail as the directory does not exist...
Your RewriteRule pattern ^(.*)$ will match all input and will rewrite to . The .htaccess rules will then be run again

apache .htaccess file https:// redirect ---- rewrite it for subdirectory?

here is my code:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
This will successfully rewrite everything to https (it will "force" https), if I put it in my home directory's .htaccess file. However, what about if I only want my subdirectory of /support to force https? How to rewrite the above code?
The intent is with regards to forcing https in WHMCS
Thanks in advance!
If you want to redirect only a subfolder and not a subdomain, that is even easier. To redirect the subfolder /support/ you would use the following code:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^/support/(.*)$ https://%{HTTP_HOST}/support/$1 [QSA]
I added a QSA flag, which will force any query string that was entered to be appended to the redirect URL. If you don't use query strings in this section of the site, you can remove that flag. Also, if the string as shown doesn't work, you can try removing the leading slash from the RewriteRule and retrying it.
Add the following rule before the %{HTTPS} line:
RewriteCond %{HTTP_HOST} ^support.domain.com$
This will only allow the rule to execute if the visitor is coming from the subdomain. Please note, these rules will likely need to be put in the .htaccess file that is the root for the subdomain, not the main site root folder.

.htaccess how to substitute urls

How can I substitute URLs of a web application in a sub folder as installed on root.
e.g. need to map
example.com/index.php
to
example.com/new/index.php
I do not want redirection but url rewrite to exempt folder name.
Help me to learn and define rules.
You can use the following rule to rewrite any request that’s URL path (without path prefix) does not start with new/:
RewriteCond $1 !^new/
RewriteRule ^(.*)$ new/$1
This will also work in .htaccess files other than in the document root directory.
But as you want to use this rule in the .htaccess file in the document root directory, you could also use REQUEST_URI:
RewriteRule !^new/ new%{REQUEST_URI}
The first line matches all requests not matching /new/... and prefixes it with a new before it.
RewriteCond %{REQUEST_URI} !^/new/
RewriteRule ^(.*)$ new/$1
Request: http://example.com/index.php
Maps to: [document root]/new/index.php