.htaccess: RewriteRule not Working using just the value - php

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.

Related

Passing parameters after creating ReWrite Rule

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]

.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.

How to do a basic URL rewrite with mod_rewrite

I don't understand why but when I use this:
#RewriteRule ^/?r/(.*)$ /index.php?n=$1 [L]
to rewrite mysite.com/r/somewhat to mysite.com/index.php?r=somewhat
the site work.
But if I use this:
#RewriteRule ^/?(.*)$ /index.php?name=$1 [L]
to rewrite mysite.com/somewhat to mysite.com/index.php?r=somewhat,
my site stop working.
I don't understand why. Someone can help me?
How can I rewrite mysite.com/somewhat to mysite.com/index.php?r=somewhat?
Your 2nd rule will cause infinite looping since target URI /index.php?r=somewhat also matches .*. Eventually it causes 500 internal server error.
To fix you need to avoid rewriting files and directories using RewriteCond like this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?(.+)$ /index.php?r=$1 [L,QSA]

.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]

changing .htaccess to skip .php from URL results in error for some web pages

I setup .htaccess file as follows to make elegant url by hiding .php extension;
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Now all of my pages work other than following three urls/pages;
www.auroracs.lk/about
www.auroracs.lk/enquiry
www.auroracs.lk/locate
for above URLS I get following error. (You may check online by trying that URL right now)
..............................
Not Found. The requested URL /about/.php was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
..............................
But if I rename about.php to about1.php then www.auroracs.lk/about1 url works. same happens to other two php files also. Can any one help why its not working only for these three pages. Is it because my .htaccess conflicting with some other file or any advice is appreciated.
Replace your rule by this more correct rule:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]

Categories