Rewrite - 2 files with same parameters and located in same folder - php

I know that was a frequent question here in SO, but I can't rewrite some dynamics URL.
Hope someone can help me.
I've 2 files located in the same folder, that receive same number of parameters:
.com/serie.php?id=__123__&nombre=__string__
.com/episodio.php?idSerie=__123__&query=__1x1__
My desired pretty url are this:
for serie.php: .com/__123__/string
for episodio.php: .com/__123__/string/1x1-some-other-strings-not-relevants-here
I was able to make the first rewrite, like this:
Options +FollowSymLinks
RewriteEngine on
# Rewrite Serie.php
RewriteRule ^([^/]*)/$ /serie.php?id=$1 [L]
RewriteRule ^([^/]*)/([^/]*)/$ /serie.php?id=$1&nombre=$2 [L]
Work fine, but the problem comes in the second rewrite.
The bold characters are "relevants" and I use this, to make some work.
I tried something like this:
RewriteRule ^([^/]*)-([^/]*)$ /episodio.php?idSerie=$1&query=$2 [L]
But I get 404 not found.
How can make this second rewrite?
Thanks in advice.

You can use:
Options +FollowSymLinks
RewriteEngine on
# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/?$ serie.php?id=$1 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/?$ serie.php?id=$1&nombre=$2 [L,QSA]
RewriteRule ^([^/]+)/[^/]+/([^-]+)-[^/]*/?$ serie.php?id=$1&query=$2 [L,QSA]

Related

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

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)

Htaccess two rules conflicting when trying to remove .php and create pretty URL

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]

Adding mod_rewrite rule causes server error for whole directory

I have a test folder (example.com/test) with it's own htaccess file. It looks like this:
RewriteEngine On
RewriteRule ^(projects|about)/?$ /test/$1.php [L]
RewriteRule ^sitemap\.xml$ /test/xmlsitemap.php [L]
RewriteRule ^([^/]+)/?$ /test/projects.php?project=$1 [L] #this line causes trouble
RewriteRule ^([^/]+)/([0-9]+)/?$ /test/posts.php?&project=$1&post=$2 [L]
RewriteRule ^([^/]+)/([0-9]+)/([^/]+)/?$ /test/posts.php?project=$1&post=$2&title=$3 [L]
When I add this line:
RewriteRule ^([^/]+)/?$ /test/projects.php?project=$1 [L]
to my .htaccess file, everything in the test directory give a 500 Internal Server Error. All the other rules work like they should.
What I'm trying to do is make example.com/test/$1 go to the example.com/test/projects.php?project=$1, with the exceptions of "projects," "about," and "sitemap.xml." There are also numbered posts in each project that have an optional title portion of the url.
The server is running apache 2.2.15.
Please let me know if you need more info or want me to test something.
Try this .htaccess in /test/ folder:
RewriteEngine On
RewriteBase /test/
RewriteRule ^(projects|about)/?$ $1.php [L]
RewriteRule ^sitemap\.xml$ xmlsitemap.php [L]
# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/?$ projects.php?project=$1 [L,QSA]
RewriteRule ^([^/]+)/([0-9]+)/?$ posts.php?&project=$1&post=$2 [L,QSA]
RewriteRule ^([^/]+)/([0-9]+)/([^/]+)/?$ posts.php?project=$1&post=$2&title=$3 [L,QSA]
The rule you have in place now should result in the following:
http://example.com/test/1 -> http://example.com/test/posts.php?&project=test&post=1
Assuming you are not seeing anything useful in access or error logs, then consider adding a rewrite log entry to the host.
RewriteLog /var/log/httpd/rewrite.log
RewriteLogLevel 9
This could help to detect something like a redirect loop which could be the cause of the internal server error.

mod_rewrite: form URL with multiple parameters to a shorter one (without middleparts)

Right now I work with these type of URLs:
http://subdomain.example.com/index.php?page=contact
http://subdomain.example.com/index.php?page=about
http://subdomain.example.com/index.php?page=category&category_id=1
http://subdomain.example.com/index.php?page=item&item_id=1
And I want them to look like this:
http://subdomain.example.com/contact/
http://subdomain.example.com/about/
http://subdomain.example.com/category-1/
http://subdomain.example.com/item-1/
So, I accommodate my page/php file into the following:
category_id -> category_name
item_id -> item_name
And my .htaccess file to this:
RewriteEngine on
RewriteRule ^([^/]*)/$ /index.php?page=$1 [L]
RewriteRule ^([^/]*)/([^/]*)/$ /index.php?page=$1&category_name=$2 [L]
Which gives me some nice URLs but I want them without the middlepart. In other words I want to get rid of "category" and "item" for example.
http://subdomain.example.com/category/category-1/ -> http://subdomain.example.com/category-1/
When I try and change my .htaccess to the following I get this to work. BUT you can see that I had to comment out the 2nd line and so my contact page doens't work any longer :(
RewriteEngine on
#RewriteRule ^([^/]*)/$ /index.php?page=$1 [L]
RewriteRule ^([^/]*)/$ /index.php?page=category&category_name=$1 [L]
I know it has something to do with the parameters, one against two but I can't figure out which way I have to follow to get this to work which makes me to have fit.
I really would appreaciate if somebody can help me out solving this!
If I understand...
RewriteEngine on
# Not for real file or directory
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# for category-1/ and all like xxxxx-xxx-nnn
RewriteRule ^([^/]+-\d+)/?$ /index.php?page=category&category_name=$1 [L]
# for contact/ or about/ or other/
RewriteRule ^([^/]+)/$ /index.php?page=$1 [L]
The result :
http://subdomain.example.com/lord-of-the-rings-1/
-> http://subdomain.example.com/index.php?page=category&category_name=lord-of-the-rings-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