i've got a problem using apaches module rewrite (browser friendly urls). I want to rewrite each request to an specified php document expecting any request containing a given string:
RewriteBase /folder/directory/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [PT,L]
How to include a condition to redirect requests including "teststring" (ignoring pre and post text) to another directory? I want to redirect jQuery ajax calls to another directory directly!
Finally got the solution: .htaccess mod_rewrite - how to exclude directory from rewrite rule
I'm not sure if I understand what exactly you want. But here's my take on it:
RewriteRule ^(.*)teststring(.*) newdirectory [R,L]
Related
Hello I have this rewrite-rule example it makes the following example:
[http://localhost/project/index.php?url=something]
on my site I use it to make
[http://localhost/project/index.php?url=task/add]
shown as
[http://localhost/project/task/add]
This is my code
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
I need to make 3 other things
make url [http://localhost/project/index.php?url=tasks/view/&page=2]
shown as [http://localhost/project/tasks/view:2]
or [http://localhost/project/tasks/view#2]
then my upload folder named (files) I wanna to redirect anyone get any
file from this folder just browser
download files on upload folder named (files) with custom url like as [http://localhost/project/getfile/1]
First of all I want to remind you that # is the 'anchor' part of an url. It is never sent to the server, so rewriting to it is probably not the best idea.
What your existing rule does, is internally rewriting all requests that do not map to an existing file, directory or symlink to index.php. If you want to rewrite urls like http://localhost/project/tasks/view:2 to be rewritten to http://localhost/project/index.php?url=tasks/view&page=2, you'll need to add this rule before the rule you already have. Otherwise the more general rule would match it before the more specific rule can.
I also presume you have your .htaccess in your /project/ directory.
make url [http://localhost/project/index.php?url=tasks/view/&page=2]
shown as [http://localhost/project/tasks/view:2]
or [http://localhost/project/tasks/view#2]
Adding the following rule before your existing rule should handle those urls nicely.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^tasks/view:(.+)$ index.php?url=tasks/view&page=$1 [QSA,L]
Alternativelly, why don't you simply explode( $_GET['url'], ':' ); in index.php instead of this rule.
then my upload folder named (files) I wanna to redirect anyone get any
file from this folder just browser
If you want to stop all direct requests to /project/files, you can use the %{THE_REQUEST} trick and the [F] (forbidden) flag. If you want to display a custom error page, add an errordocument handler for the forbidden status code.
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /project/files
RewriteRule ^ - [F,L]
download files on upload folder named (files) with custom url like as
[http://localhost/project/getfile/1]
Remember that for every request you send to the server, Apache will match it against a regex in the RewriteRule and either rewrite it internally or redirect the user. To internally rewrite a request to /project/getfile/1 to /project/files/1 you can use the following rule. Add it before the rule you already have.
RewriteRule ^getfile/(.*)$ /files/$1 [L]
I encourage you to read the documentation for mod_rewrite.
I've looked around on here to try and find a solution to my problem with no avail. Or I may have missed it.
Okay so I have a .htaccess rewrite rule setup:
# redirect all calls to index.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1 [PT,L]
</IfModule>
Which rewrites all mysite.com/?page=thisroute/thispage or mysite.com/?page=thispage to: mysite.com/thisroute/thispage or mysite.com/thispage
Is there a way to pass custom POST variables as well? How could I pass a form through this format without calling the php file that processes it directly?
Ex: mysite.com/thispage/?name=scott&this=that etc.
here is my website
http://www.coolcodez.net/ios/nicucalc
notice when you click on pages on the nav you get urls like
http://www.coolcodez.net/ios/nicucalc/index.php?page=features
I put an .htaccess file in my nicucalc directory. I want the urls to look like this
http://www.coolcodez.net/ios/nicucalc/features
even better would be
http://www.coolcodez.net/nicucalc/features
here is my htaccess file. It's never working properly..
RewriteEngine on
RewriteCond %{QUERY_STRING} ^page=(.*)$
RewriteRule ^index\.php$ /%1/? [R=301,L]
what am i doing wrong. explanation as well please
also note: this folder is located inside of a wordpress installation folder. not sure if that htaccess file would be affecting mine somehow
The rule that you have redirects requests for index.php to /features/ (or whatever the "page" is). This is fine in and of itself but you need something that rewrites it internally back to index.php. Because of that you need 2 rules, one to redirect (matches request) and one to internally rewrite (matches URI):
RewriteEngine On
RewriteBase /ios/nicucalc/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /ios/nicucalc/index\.php\?page=([^&\ ]+)&?([^\ ]*)
RewriteRule ^ /ios/nicucalc/%1?%2 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1 [L,QSA]
The first rule matches against the request, this looks like:
GET /ios/nicucalc/index.php?page=features HTTP/1.1
The "features" is captured and backreferenced in the rule using %1. The second rule first checks if the request points to an existing file or directory. If it doesn't then the URI is captured and then rewritten to index.php and the URI gets passed to the script via the "page" parameter.
I have the following in my htaccess file:
RewriteEngine On
RewriteRule ^(.*)$ rewrite.php?data=$1 [L,QSA]
From rewrite.php I redirect to the correct pages depending on the url. Problem is that it redirects all files including css and js. I tried including these files but I now realise that was dumb of me. Should I redirect when there is and appropriate extension in the url? If redirecting is the way to go what method would be best? header location or HTTP_redirect?
Or is this not a good idea performance or work involved wise? I could go for something like this but I know next to nothing about apache and would rather not work with it right now.
RewriteRule ^(.*).css$ /includes/compressor.php?i=$1.css [L]
I previously had the following in my htaccess file:
RewriteCond %{REQUEST_FILENAME} !-f
I decided to remove this because:
I would not be able to include the header and other common files in the rewrite.php file. I would also not be able to have a database call in the rewrite file that would determine the page to include and to reuse the data for the page contents.
Unwanted files would be reachable such as service used only by external app.
The compression should be done once, and not for every request. You can then exclude requests from the URL rewriting if the corresponding file exists:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ rewrite.php?data=$1 [L,QSA]
How about redirecting only if the requested file does not exist on the server?
You could use the following rewrite conditions to achieve this.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*)$ rewrite.php?data=$1 [L,QSA]
So if the request is for a CSS/JS/HTML/Image file that already exists on the server then no rewriting takes place and the request for the file is processed. If the file does not exist, it will run your rewrite rule to rewrite.php
I have set up my .htaccess to redirect calls to mydomain.com/something.html to mydomain.com/index.php?q=something
This works fine.... but I noticed that since moving to a new hosting I am getting multiple queries for the query "missing".
I am pretty sure it is due to the default html page for wrong URLs being "missing.html" which is redirected as mydomain.com/index.php?q=missing. So any missing URL will cause my PHP script to be run with "missing" as input.
Is there a way to keep the URL redirect and manage the calls to missing.html without actually calling my PHP script for missing URLs?
Edit with solution; here is my .htaccess which does the redirect only if there is no existing URL:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*)\.html index.php?q=$1
Seems the RewriteCond need to be placed before (as shown above).
You're looking for RewriteCond.
Try this in your .htaccess:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
These rules will also stop rewriting for anything is an accessible path. If you want strictly missing.html, then the following rule will be sufficient:
RewriteCond %{REQUEST_URI} !/missing.html
Note these directives must come before RewriteRule.
Isn't is possible to add a rewriting rule which takes precedence over the other one and specifically rewrites mydomain.com/missing.html to the location of your 404 page? (Seing the redirection rule you already set up would help.)