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.
Related
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]
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]
I am trying to remove the .php extension at the same time as creating a "pretty URL" for a certain page.
I am trying to turn domain.com/dox?id=3 into domain.com/3
Current rewrite conditions in my htaccess file:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^([^/]*)\$ /dox?id=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]
# remove index
RewriteRule (.*)/index$ $1/ [R=301]
This creates an error at domain.com/3 that says:
Not Found
The requested URL /3.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.
However if I add any extension to the code, it works. To explain what I mean, if I change the fourth line to this:
RewriteRule ^([^/]*)\.extension$ /dox?id=$1 [L]
Then the page domain.com/dox?id=3 will be accessible at domain.com/3.extension
Obviously the problem is the two rules are conflicting, but not being a htaccess whizz, I can't figure it out
Try this:
RewriteRule ^([A-Za-z0-9-]+)/?$ /dox?id=$1 [L]
Or if id is just a number then this would be more appropriate:
RewriteRule ^([0-9]+)/?$ /dox?id=$1 [L]
In my single-page-webapp i used the html5 history API so that urls could have a REST pattern (/section1/stuff1..) and i'm planning to make a sort of a javascript router to navigate to several sections of the page depending on the url path.
Right now i'm still working on a local server(wamp) and i added a .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) index.php/$1 [L]
to the app root for that url's paths containing references to some sections of the page (e.g subdomain/sectionN) can redirect always to the index.php and the redirection was successful BUT all external ressources failed to load and i got :
Resource interpreted as Image but transferred with MIME type text/html: "http://localhost/subdomain/section1/images/imgname.gif".
and it's logic because the images folder lies in the app root and not under a /section1 folder and the .htaccess rule RewriteRule (.*) index.php/$1 [L] should take only the /images/imgname.gif part and concatinate it after the http://localhost/subdomain/.
I found this as a similar problem so i rewrite the .htaccess file like this :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?section1/(.+)$ index.php/$1
RewriteRule (.*) index.php/$1 [L]
but i got a 500 Internal Server Error.
These two rules:
RewriteRule ^/?section1/(.+)$ index.php/$1
RewriteRule (.*) index.php/$1 [L]
will potentially both be applied to a single URL, because there is no [L] (last) tag after the first rule. So an URL like this:
section1/stuff/page1.html
will be converted to this by the first rule:
index.php/stuff/page1.html
and then will get fed into the second rule and converted to this:
index.php/index.php/stuff/page1.html
This is most likely what is causing the 500 internal server error. If you add [L] to the first rule then the second rule won't be applied in the case that the first rule matched the URL and was applied:
RewriteRule ^/?section1/(.+)$ index.php/$1 [L]
If you don't want your image URLs to be rewritten, then just remove the second RewriteRule (which actually makes the [L] redundant).
My code:
RewriteEngine on
RewriteBase /
RewriteRule ^article/([a-z]+)/?$ index.php?page=article&name=$1 [L]
RewriteRule ^([a-z]+)/?$ index.php?page=$1 [L]
I am using WAMP and had setup a Virtual Host.
In my index.php, there is code to get page passed and checks if it exists(in database). If not, display an error message. It works fine.
Eg: http://mysite/contactus/
But it will not work if I use a a directory name as page_name in the URL. Eg: http://mysite/images/. This will display page not found error (ie. checks database and no page found, so display "not found"). But it will not display images,css(linked file) in the page. Also, it shows http://mysite/images/?page=images in addressbar.
Like that, if I goto js folder which is used to store javascript files, above problem occurs. So, problem is caused if any subdirectory's name is passed as pagename.
How to solve this ?
When http://mysite/images/ is supplied, mod_rewrite is redirecting to http://mysite/images/index.php?page=images instead of http://mysite/index.php?page=images
Edit
Please tell me how to block hotlinking of files and directory, and redirect back to index page or send some browser header error ?
I tried this:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*) http://%{HTTP_HOST} [R,L]
RewriteRule ^article/([a-z]+)/?$ /index.php?page=article&name=$1 [L]
RewriteRule ^([a-z]+)/?$ /index.php?page=$1 [L]
Edit
New code(semi-working):
RewriteEngine on
RewriteBase /
# remove trailing slash ONLY if it is not an existing folder
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1/ [R,L]
RewriteRule ^article/([a-z]+)/?$ http://%{HTTP_HOST}/index.php?page=article&name=$1 [L]
RewriteRule ^([a-z]+)/?$ http://%{HTTP_HOST}/index.php?page=$1 [L]
This code will clear the problem with not displaying pics and css when a directory name is mentioned. But whatever pagename i specify eg:http://mysite/contactus, it will goto URL: http://mysite/index.php?page=contactus. Even if I use a directory name eg: http://mysite/js, it will goto: http://mysite/index.php?page=js
I am very confused.
RewriteEngine on
RewriteBase /
RewriteRule ^article/([a-z]+)/?$ index.php?page=article&name=$1 [L]
RewriteRule ^([a-z]+)/*$ /index.php?page=$1 [L]
you have to put the slash in front.
Edit: changed the ? to *
My understanding is that your script is for documents only, not images or other resources.
Then you should ignore them right away. Try adding this line right after RewriteBase like this :
RewriteEngine on
RewriteBase /rewrite/
RewriteRule ^/(images|js)/(.*)$ - [L]
RewriteRule ^article/([a-z]+)/?$ index.php?page=article&name=$1 [L]
RewriteRule ^([a-z]+)/?$ index.php?page=$1 [L]
Then these subdirectories would be served right away, thus bypassing the next RewriteRule set.
For the problem with the directories I usually force a slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+[^/])$ $1/ [R]