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.
Related
I am creating a rest api endpoint to input data into a database. I have been trying to rewrite the url into this format http://example.com/src/public/endpoint/data. The public in the url is a folder that had has an index.php file that all the urls will be routed to but it is not working. Below is my .htaccess code.
RewriteEngine
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://example.com/src/public/index.php?path=$1 [NC,L,QSA]
I am getting the link address http://example.com/src/public/?path=state/127&_=1579497796272 as the endpoint when i expect example.com/src/public/state/127. I know am not doing something correctly but i can not figure it out.
Try this :
RewriteEngine On
RewriteCond %{THE_REQUEST} \?path=(.*)&(.*)\sHTTP.*$
RewriteRule ^(.*)$ /src/public/%1? [L,R]
RewriteRule ^src/public/state/127 /src/public/?path=state/127&_=1579497796272 [L]
First you should add on after RewriteEngine which means enabling mod_rewrite .
The second line will captrue the request and get a part of query string.
Third line will externally redirect the request to be friendly.
The last line will redirect a new one, internally , to the correct path.
Note: if it is ok , change R to R=301 to be permenant redirection.
Some servers creates subdomains folders in the same folder as main domain.
As a result if a main domain has news.php page rewritten to /news (just to remove php extension for SEO reasons) and /news folder existing at the same time, the user is being redirected to /news folder instead of news.php file.
I could redirect specific folders to a file with:
RedirectMatch 301 ^/plotki/?$ /plotki.php
but how to redirect all of them to the same filename as directory with some exception like /tags, /scripts etc. and all php files in main directory ?
If I understand the problem correctly: when a file exists in the main folder, you don't want a rewrite. However, if a folder is matched, it should also not rewrite to one file.
Lets assume you want all traffic to "index.php" but dont want the file "news.php" (which is in the main folder) nor the folder "/include/" to be redirected to index.php
The easiest way to do this, would be with a rewrite rule. For that you first need to turn on the rewriteEngine in the .htaccess file.
RewriteEngine On
The rewriteEngine works with conditions and rewrites. In your case there could be multiple contitions that should be met, prior to rewriting. If one of the conditions is not met, don't rewrite.
The below line tests if the "Request URI" contains the text "/include/". Only if it does NOT contain that in the url, it will let you continue to the next line and finally rewrite. You can place all exceptions like the below example. Note that you can use regular expression for this! [NC] means "No-case" more info
RewriteCond %{REQUEST_URI} !/include/ [NC]
RewriteCond %{REQUEST_URI} !(/include/|/folder 2/|/folder[0-9]{2}) [NC]
After you place all exceptions as a rewrite condition, you want to make sure that if the file exists on the server, the rewrite rule does not apply. This is done with the line containing "!-f". Lastly you must make sure that the rewrite rule is not applied when the request URL already IS the index.php file (else you will get an internal, ethernal loop, and a 505 error). The final line sends all data to index.php. Note that I do not send any additional parameters to index.php. In index.php you can read $_SERVER['REQUEST_URI'] to obtain all data you need!
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !index.php [NC]
RewriteRule ^(.*)$ /index.php [L]
You can use http://htaccess.madewithlove.be/ to test your htaccess conditions. The script will tell if a condition is met or not. Note that the website does not support all variables like %{REQUEST_FILENAME}
==Edit==
If you only want to redirect traffic to /page/... to page.php, but leave all other traffic as is you can make one more condition.
RewriteCond %{REQUEST_URI} !/include/ [NC]
RewriteCond %{REQUEST_URI} ^/page/ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !page.php [NC]
RewriteRule ^(.*)$ /page.php [L]
^/page/ means: ^ represents the beginning of the string, so, the Request URI must START with /page/ in order to match the condition. If it does so, it will continue and rewrite that url to page.php
We have added a paging system inside our layout. When we go to /page/clan, the page about our clan gets displayed. (as its located in pages/clan.php).
To get /page, we used a htaccess script, which rewrites index.php?page=pagename into the /page/pagename I mentioned.
This is our current htaccess code for converting these urls:
RewriteEngine On
RewriteRule ^page/([^/]*)$ index.php?page=$1 [L]
However, We'd like to remove the /page part, so it's possible to just use /clan instead of /page/clan to open the clan page.
How can this be done and with what code?
Thanks!
Try :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)/?$ index.php?page=$1 [L,NC]
Rewrite condions make sure you don't rewrite any existing files or directories on the server.
[NC] flag makes the pattern case insensitive, so in you case example.com/fOo would also work.
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 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]