URL rewriting in .htaccess doesn't works as it should - php

I am currently coding my own website in PHP and MySQL on WampServer (local). I've already rewrited some of my URLs successfully. But I'm having problems with one of them.
I want to display on a page all my articles listed in a category using the GET method. For the moment, I have the following URL : http://localhost/actuco/cat.php?id=xpS3cc&slug=amerique-du-nord and I would like to use and display this URL as http://localhost/actuco/c-xpS3cc-amerique-du-nord/ (which does contains exactly the same GET parameters than the original URL).
I have tried to do this with the following line in my .htaccess file
RewriteRule ^c-([^/]*)-([^/]*)/$ cat.php?id=$1&slug=$2
When I write the second URL in my browser, it shows me a blank page with no code lines at all. My first URL works perfectly.
I'm really lost and I really don't know how to fix it.
Here is the whole .htaccess file used on my website (all other URL rewritings in this file do work).
Options +FollowSymlinks
RewriteEngine On
RewriteBase /actuco/
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_URI} /+[^\.]+$
#RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/$ article.php?lng=$1&yr=$2&mo=$3&dy=$4&slug=$5&total_slug=$6
#RewriteRule ^([^/]*)-([^/]*)-([^/]*)$ waluty.php?cur=$1&amt=$2&lang=$3
RewriteRule ^([^/]*)/([^/]*)$ url.php?mode=$1&u=$2
RewriteRule ^c-([^/]*)-([^/]*)/$ cat.php?id=$1&slug=$2
RewriteRule ^bio$ o.php [L]
Thanks in advance for your help !

just pest this code in your .htaccess file
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php?/$1 [L,QSA]
hopefully, it will work.

Solved ! Just forgotten to add slash before $ in this line
Before :
RewriteRule ^([^/]*)/([^/]*)$ url.php?mode=$1&u=$2
After :
RewriteRule ^([^/]*)/([^/]*)/$ url.php?mode=$1&u=$2
Now it works, but I still have a problem with multiple hyphens in slug described here : Multiple hyphen handling in .htaccess (URL rewriting)

Related

How rewrite TWO URLS for .HTACCESS?

One pages of site have such adds: domain.com/posts/name.php
Other pages of site have such adds: domain.com/pages/name.php
I need to cut from these adds posts/ and pages/ only.
Firstly, I tried to use in .htaccess next rules:
RewriteRule ^([A-z0-9-]+)$ /posts/$1
RewriteRule ^([A-z0-9-]+)$ /pages/$1
Don't help.
Secondly, I tried to use in .htaccess such rules and conditions:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ posts/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ pages/$1 [L]
As result: image of mistake 404
Also I tried to use such method:
RewriteCond %{REQUEST_URI} ^([A-z0-9-]+)$/(posts|pages)/([A-z0-9-]+)/$1 [NC]
RewriteRule .* domain.com/%1%3
Don't help....
what i am understanding you need to remove the post and pages from the url following code will help you for that.
You would need the rewrite module of Apache: mod_rewrite.
Then do something like this:
RewriteEngine on
RewriteRule ^post/(.*)$ $1
RewriteRule ^pages/(.*)$ $1
Here is the official documentation of mod_rewrite: click
please test it onece I dint test the code but did same for replacing url and its work for me.
Thanks.
The solution of this problem such:
We enter all information about the page in the MYSQL database:
image of database
We generate a page from the database:
generation of page (php)
Set the generated page for Human-Friendly Url:
file .htaccess
So everything works, and POSTS there are in their folder in the database, and PAGES in their folder.
folder in database
All is clean. If you can do better somewhere, then correct me, please.

htaccess rewrite subdirectoy to file

I have a problem with rewriting urls to my files. What I am trying to do is making my little shop system a bit more SEO friendly. My problem is that it sometimes works and sometimes it doesn't. I have no idea what I should do or what I am doing wrong.
My UPDATED .htaccess file:
ErrorDocument 404 /shop/404.html
RewriteEngine On
RewriteBase /shop/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^produkte/?(.*)$ products.php$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^produkt/?(.*)$ product.php?url=$1 [L]
RewriteRule ^(.*)/(css|js|img|fonts)/(.*)?$ /shop/$2/$3 [L,QSA,R=301]
For example the /shop/products link is not working but /shop/products/ is.
And for some reason if I want to open the link /shop/products?cat=besteck its redirecting me to: localhost/D:/xampp/htdocs/shop/products.php?cat=besteck but If I capitalize the b it's working fine..
I have no Idea what to do, please help me! (And dont just give a working code snippet explain why mine fails and yours works)
EDIT:
Just to clear things up I want /products, /products/ and /products?some_get_query to redirect to my products.php file. /product/some_seo_url should be redirected to product.php?url=some_seo_url. I tried adding a question mark after the forward slash in my RewriteRule and I also tried putting the ^products rule above the ^product rule. Nothing worked yet.
EDIT 2:
I updated my .htaccess code above and now nearly everything works. The only thing that still doesn't work is when I open /shop/products/?cat=fish or /shop/product/some_product, my resources aren't loading!
ErrorDocument 404 /shop/404.html
RewriteEngine On
RewriteBase /shop/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^products/?(.*)$ products.php?$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^product/?(.*)$ product.php?url=$1 [L]
RewriteRule ^(.*)/(css|js|img|fonts)/(.*)?$ /shop/$2/$3 [L,QSA,R=301]
First off your product rule also matches products so a rewrite like:
products/cat/fish becomes product.php?url=s/cat/fish
Which is not what you want, the easiest way to avoid that is to reverse the order so that the products rewrite comes before the product one but I've also added the Last flag ([L]) to be on the safe side; besides, once it's got the match you want it's better for it to stop looking.
To prevent recursive rewrite loops you need to specify that the rewrite only occurs when the redirect is not an existing file or directory (otherwise your product rewrite matches product.php and it loops - forever). That's what those RewriteCond lines signify.
Other than that it seems OK.

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

Redirecting existing URLs to SEO Friendly URLs

I have came accross a problem that every .htaccess query I've tried wasn't worked out ! I have URLs like this:
http://www.example.com/index.php?x=product
And I want to change it to a user friendly URL like this:
http://www.example.com/product/
Or it can be:
http://www.example.com/product.php
I've tried this code below:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^x=product$
RewriteRule ^index.php.*$ http://www.example.com/product.php? [R=302,L]
Now, it is redirecting perfectly, but this is not the problem. I've used this for only SEO so I must include http://www.example.com/index.php?x=product in the product.php file. Any help can be precious, thanks...
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} /index\.php\?x=([^\s&]+) [NC]
RewriteRule ^ /%1/? [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/.]+)/?$ index.php?x=$1 [L,QSA]
This will redirect /index.php?x=product to /product/ and will rewrite it internally to /index.php?x=product in 2nd rule.
You don't need to put anything in the product.php file. Make sure there is a .htaccess in the directory that has the files you want to make url/seo friendly.
To have it SEO friendly make sure its in this format
http://www.example.com/product/ not http://www.example.com/product.php
if you must have a file extension, have it in http://www.example.com/products.html (you want to tell the search engine the page isn't dynamic although it is to get better pagerank)
Insert this in your .htaccess
RewriteRule /(.*)/$ products.php?x=$1
the (.*) pulls the elements out and puts them in variables $1

Issue with mod_rewrite when accessing directory

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]

Categories