I want to redirect traffic for files/dirs that do not exist, however my router does something strange with uris that have two filenames. So I want to explicitly change the uri example:
https://example.com/testsite/index.php => https://example.com/testsite?returnPath=/index.php
Then internally, I would redirect to https://example.com/new/index.php/testsite
my root structure looks like so:
-web:
.htaccess
-new:
index.php
My htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{QUERY_STRING} !returnPath=*
RewriteRule ^([A-Za-z]*)/(.*)$ /$1?returnPath=/$2 [QSA,NC,R=302,L] #explicit redirect (what other flags do I use here?)
RewriteCond %{REQUEST_URI} !-d
RewriteRule (.*) /new/index.php/$1 [QSA,L,NC]
Obviously leaving in the L flag in the first Rule will write the redirect without processing any other Rules.
But leaving it out, processes the the second Rule also but doesn't change the shown uri
Related
I havve been using an htaccess file to create rewrites so as not to not have to include .php/.html filenames in my urls.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example-page.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example-page.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule /front /front_page.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule /redirect /redirect-page.php
However as my code is currently written the redirects are not working and all attempts at loading the pages with anything else besides the filenames is giving me a 404 error. How might i fix my code to get the redirecting to work?
Write the directives like this instead:
RewriteEngine on
RewriteRule ^front$ /front_page.php [L]
RewriteRule ^redirect$ /redirect-page.php [L]
There doesn't seem to be a need for all your additional conditions? I assume you only have the one domain example-page.com and you don't have a file called /front etc.
In .htaccess, the URL-path matched by the RewriteRule pattern does not start with a slash. You should also include start/end-of-string anchors on the regex so that you only match front exactly and not match it anywhere in the URL-path.
I have a set of rules in a .htaccess file that redirects all requests to index.php, but this is interfering with AJAX requests that need to access the requested file directly.
I am trying to resolve this by appending ?ajax=true to all AJAX request URLs, and creating a rule that says "all URLs ending in ?ajax=true should be processed directly".
Here is my .htaccess file currently:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{QUERY_STRING} !^ajax=true$
RewriteRule ^(.*)$ index.php?params=$1 [NC]
My AJAX request URL would be as follows:
http://www.example.com/process_ajax.php?ajax=true
No luck so far.
I think you are close with using a negative RewriteCond but the syntax could be a bit off. Try using parentheses like in my example below.
At first, I would use a wildcard value for the GET parameter like .* so you can focus on targeting the correct key. Once it's working you can test again for getting the right value, presumably true.
Try:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{QUERY_STRING} !(ajax=.*)$
RewriteRule ^(.*)$ index.php?params=$1 [NC]
I'm having some problems rewriting URLs with the following rules
RewriteEngine on
RewriteRule ^page/(.*)$ index.php?pag=cms&title=$1 [NC]
RewriteRule ^admin/(.*)$ admin/$1 [NC]
RewriteRule ^(.*)$ index.php?pag=$1 [NC,L]
What I'm trying to achieve is to check if the URL is a cms page or not and leave admin URLs as they are.
If I remove the last condition it works but I will have no rule for not cms pages.
Ideally I would want to have just one rule for every page (cms or not) but I can't figure out how to check that other than using page/ in the URL.
Mod_rewrite will keep looping through all the rules until the URI stops changing (or it reaches its internal redirect limit, resuling in a 500 error). You need to add a few conditions to the last rule so that it won't rewrite URI's that's already been properly routed:
RewriteRule ^page/(.*)$ index.php?pag=cms&title=$1 [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?pag=$1 [NC,L]
Additionally, the second rule does nothing except a passthrough, so you can replace it with
RewriteRule ^admin/(.*)$ - [NC,L]
You need these rules:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
From the Apache documentation:
'-d' (is directory)
Treats the TestString as a pathname and tests whether or not it exists, and is a directory.
'-f' (is regular file)
Treats the TestString as a pathname and tests whether or not it exists, and is a regular file.
I have one hosting account that I am running using to run multiple sites, masterhost.com. Under masterhost.com I have site1.com, site2.com, etc... and am using this piece of code in the masterhost directory to reroute the domain request from the masterhost directory to the site1 directory.
RewriteCond %{HTTP_HOST} ^(www.)?site1.com$
RewriteCond %{REQUEST_URI} !^/site1/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /site1/$1/
RewriteCond %{HTTP_HOST} ^(www.)?site1.com$
RewriteRule ^(/)?$ site1/index.php
However Joomla on site1.com is having some issues with with this, and is routing the default images path back to masterhost.com/images.
Is there a rewrite I could use that would recognize any request http://www.site1.com/images and write it to http://www.masterhost.com/site1/images?
I believe the problem is these two lines:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Basically, if the image exists on mainsite.com, these rules will prevent the request from being redirected to site1. So, for example, if you have mainsite.com/img/logo.png and site1.com/img/logo.png, site1.com would show the logo for mainsite.com.
Also, with this rule:
RewriteRule ^(.*)$ /site1/$1/
I don't believe the trailing slash should be there.
Finally, you didn't ask about it, but the last rule probably also doesn't work? If it does, removing the !-f and !-d checks above will break it. This would be because if a user requests site1.com/, the first rule redirects that to /site1/, which will never match the rule ^(/)?$. So, that rule needs to be changed to:
RewriteRule ^site1(/)?$ site1/index.php
You may or may not need a slash between ^ and site1 in that rule, depending on your RewriteBase directive.
i have a strange apache mod_rewrite problem. I need to hide a sub-directory from the user, but redirect every request to that sub-directory. I found several quite similar issues on stackoverflow, but nothing really fits, so i decided to post a new question.
My .htaccess looks like this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)?$ foo/$1 [QSA,L]
The document-root only contains the following folder/files:
/foo/bar/index.html
I would now expect that example.com/bar and example.com/bar/ would just show me the contents of index.html.
Instead example.com/bar/ show me the content as expected but example.com/bar redirects me with a 301 to example.com/bar/foo/ an then shows the contents. I really don't get why there is a 301 redirect in this case.
When i put something this
RewriteCond %{REQUEST_URI} !^[^.]*/$
RewriteCond %{REQUEST_URI} !^[^.]*\.html$
RewriteCond %{REQUEST_URI} !^[^.]*\.php$
RewriteRule ^(.*)$ $1/ [QSA,L]
on top of that rule it seems to work, but that would require me to list every used file extension...
Is there any other way i can omit the redirect, the folder "bar" should never be seen by an outside user.
Thanks in advance!
1st rewrite rule is redirect from /foo/(.) to ($1) and second - from (.) to $1.
just idea, this has not been tested.
Better late than never...
Got it working with a simple RewriteRule which append a / to every url that doesn't have on.
# only directories
RewriteCond %{REQUEST_FILENAME} !-f
# exclude there directories
RewriteCond %{REQUEST_URI} !^/excluded-dirs
# exclude these extensions
RewriteCond %{REQUEST_URI} !\.excluded-extension$
# exclude request that already have a /
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ /$1/ [R=301,L]