Passing parameters after creating ReWrite Rule - php

Does anyone know how i can stop variables from being lost after creating a Rewrite Rule?
Putting in [QSA] gives me a Server 500 error message
I want this rule
RewriteRule ^family-name/([^/.]+)/([^/.]+)$ family-name.php?familyName=$1&token=$2 [L]
and this works in the sense of i get
family-name.php/somthing/token
However, when i go to $_GET['familyName'] or token it returns blank. why?
Note. the ReWrite rule is in my .htaccess page
UPDATE:
I have reduced my code down to this:
RewriteEngine On
RewriteRule ^my-family/family-name/([a-z]+)/([a-z0-9]+)$ my-family/family-
name.php?familyName=$1&token=$2 [QSA,L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)$ $1.php [NC,L]
My parameters are being passed through onto the next page LOCALLY ONLY and not when uploaded to my live site. Any thoughts???

You should open page (with your domain name).
http://example.com/family-name/something/token
And call your parameters with $_GET['token'], not $_SESSION['token'].
If you want RewriteRule for
http://example.com/family-name.php/something/token
You should have:
RewriteRule ^family-name.php/([^/.]+)/([^/.]+)$ family-name.php?familyName=$1&token=$2 [L]

Related

Why the url in which index.php is prefix with first segment in codeigniter works | Codeigniter | RewriteCond

I am working on a CodeIgniter project and today I found a very strange issue.
When I open the URL that is prefixed with index.php in the first segment it is still working even though I expect the URL to return a 404 Not Found page.
For example, the URL of my website is http://localhost/project and when I open the URL http://localhost/project/jobs it works fine, but when I open http://localhost/project/index.phpjobs it also works.
I don't know what is going on over here!
Please note that the URL doesn't include slash but is still working and that is not a typo.
Please check in your project and let me know if someone have the same problem because I think this problem may also exist in your current project but not noticed.
.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^admin(.*)$ admin/index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
RewriteRule ^.well-known/ - [L,NC]
Your first rewrite rule
RewriteRule ^admin(.*)$ admin/index.php?/$1 [L]
will be honored only if the previous two conditions
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
have been met.
Now, the trick lies in these conditions which basically say that the rewrite will be performed only if the requested resource (REQUEST_FILENAME here) does not exist as either a file or a folder.
Since index.php obviously exists the rewrite rule is skipped and the server actually receives the original (non-rewritten) request.
That is the reason why you see the same result for requests that both do and do not contain /index.php/ as prefix.
The same applies for both sets of rewrite, the one you are using for your admin page and the regular one.

Is there a way to create a friendly url like site.com/state/127 and still point to a particular file like index.php

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.

.htaccess filepaths differing how to solve

I've made www.site.com/asd to redirect to www.site.com/index.php?page=asd. However, I would like to add one more thing, where www.site.com/products/asd would redirect to www.site.com/index.php?page=products&item=asd
I can't seem to get it working, I have this as my base code:
RewriteEngine On
RewriteRule ^(admin|pictures)($|/) - [L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_URI} !=/pictures/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /index.php?page=$1 [L,QSA]
I've tried adding:
RewriteRule ^([^/]*)/([^/]*)$ /index.php?page=$1&item=$2 [L]
in various locations of the .htaccess file, which either results in:
500 Internal Server Error
or
0 CSS files load in .com/products/asd And no pictures load in .com/anyfile
I understand it's path related, but is this a good way to go about it? Can I solve it somehow in the .htaccess file, or would I need to add some check that if it's product page and a product is shown, include ../css?
The ^(.+)$ regular expression gives you a "500 Internal Server Error" because the redirection matches to the rule. So you gets the following error:
Request exceeded the limit of 10 internal redirects due to probable configuration error.
You can use:
RewriteRule ^products/(.+)$ index.php?page=products&item=$1 [L,QSA]
RewriteRule ^([^\.]+) index.php?page=$1 [L,QSA]
So, the following URLs will be redirected to:
/products/asd to index.php?page=products&item=asd
/asd to index.php?page=asc
Change the last two lines like this :
RewriteRule ^([^/]+)/?$ /index.php?page=$1 [L]
RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?page=$1&item=$2 [L]
You should specify that in first rule a URI must come like /whatever/ or /whatever so the second will match either /whatever/whetever or /whatever/whatever/ and both will work individually without any interference.

.htaccess ReWriteRule - Requested URL <rewritten filename> was not found on this server

I've been testing this in WAMP and I can't get it to work. I believe WAMP is set up properly due to the error message I'm receiving and it works no problem when I don't use the .htaccess file.
I have a filename that I'm using for testing called Feedback.php. Instead of displaying as www.mysite.com/Feedback.php. I'm trying to get it to just be www.mysite.com/Feedback.
.htaccess file
RewriteEngine on
RewriteRule ^Feedback.php/?$ Feedback [NC]
The error message that I receive is
"Not FoundThe requested URL /Feedback was not found on this server."
Are there two files required for reWriteRule to work?
I'm also trying to get this to work for my index.php to just be www.mysite.com which may be a different monster.
What should my navbar links be? Right now they are Feedback Should href perhaps be href="Feedback" once I get this working?
EDIT: I had the ReWriteRule variables backwards. Instead of
RewriteRule ^Feedback.php/?$ Feedback [NC]
I should have
RewriteRule ^Feedback/$ /Feedback.php [NC,L]
The rewrite doesn't look to have taken place though as it still reads localhost/Feedback.php
You may use this to remove .php extension from your pages as well as omitting the index part to your website:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{THE_REQUEST} /index
RewriteRule ^index$ http://yourwebsite.com/ [R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]

.htaccess: RewriteRule not Working using just the value

I've done a lot of research to solve this problem but I still can't figure out why it won't work. Initially I had Re-written my urls to add a .html but I want to change that and keep them simple without any suffix.
Here's an example of original link
http://www.howwe.biz/artist?a=eddy-kenzo
I used the rule below to change the link above and add a suffix .html
RewriteRule ^([^/]*)\.html$ /artist?a=$1 [L]
The result is : http://www.howwe.biz/eddy-kenzo.html
I want to rewrite the original link to something like this
http://www.howwe.biz/eddy-kenzo
I've tried using RewriteRule ^([^/]*)$ /artist?a=$1 [L] to achieve that but I get a horrible server 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#howwe.biz 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. Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.`
The eddy-kenzo in the url is the value. What's odd is that if I add the name which in this case is a, it works. See below:
RewriteRule ^a/([^/]*)$ /artist?a=$1 [L] is valid for http://www.howwe.biz/a/bebe-cool but I need this like this http://www.howwe.biz/bebe-cool
What is it that am doing wrong. Your help will be much appreciated.
Sorry if this was tl;dr.
This should work :
RewriteEngine on
#1redirect "/artist?a=foo" to "/foo"
RewriteCond %{THE_REQUEST} /artist\?a=([^\s]+) [NC]
RewriteRule ^ /%1? [NC,L,R]
#skip the rule if the request is for dir
RewriteCond %{REQUEST_FILENAME} !-d
#skip the rule if the request is for file
RewriteCond %{REQUEST_FILENAME} !-f
#rewrite anyother request to "/artist?a="
RewriteRule ^([^/]+)/?$ /artist?a=$1 [NC,L]
Your rule
RewriteRule ^([^/]*)$ /artist?a=$1 [L] failed because it is rewriting any request to target url unconditionally , the target url matches the pattern and it rewrites to itself. You rule will work with the following conditions:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)$ /artist?a=$1 [L]
Try this:
RewriteRule \/(.*)? /artist?a=$1
It takes everything from behind the last / to the end, and adds it to $1.

Categories