mod_rewrite issue with parameters - php

I have the following .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^portfolio-single/([^/.]+)/?$ portfolio-single.php?id=$1 [L]
<Files ~ "^\.(htaccess|htpasswd)$">
deny from all
order deny,allow
</Files>
Options -Indexes
ErrorDocument 400 /error.php
ErrorDocument 403 /error.php
ErrorDocument 404 /error.php
ErrorDocument 405 /error.php
ErrorDocument 408 /error.php
ErrorDocument 500 /error.php
ErrorDocument 502 /error.php
ErrorDocument 504 /error.php
Currently all pages that end with the php extension are working fine. So for example www.somewebsite.com/blog.php is now accessable via /blog. However I want a certain page, namely, portfolio-single.php?id=123 to be accessible via /portfolio-single/123. The 4th line you can see code that I took from this website, and I have been trying to get it to work but keep getting a 404 when I access portfolio-single via /portfolio-single/123. What am I missing? A follow up question is if I have to reboot XAMPP for changes to the .htaccess file to take effect (so far it has worked without).

Try swapping the two rules. The last rule is more specific so it should match first.
While your current approach works, it's more common to use one catch-all rule and a generic handle script which does the routing.
RewriteRule ^(.*)$ handler.php?path=$1 [L]
Then handler.php can decide what to do with blog and portfolio-single/x and non-existing-page.
The router can now be implemented in PHP which gives you more freedom, like dynamically adding routes and matches based on more than a regexp.
Another advantage of this approach is that your application now has a single point of entry. This makes bootstrapping much easier since there is only one path.

For adding .php extension you must check whether corresponding .php file exists firts:
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [NC,L]
RewriteRule ^portfolio-single/([^/.]+)/?$ portfolio-single.php?id=$1 [L,NC,QSA]

Related

404 error page not redirected to custom page using htaccess

My htaccess rule to redirect 404 error to custom page isn't working.
I put the below code:
<IfModule security2_module>
ErrorDocument 404 https://example.com/404.php
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule index\.html$ index.php [L]
</IfModule>
And I used both - ErrorDocument 404 https://example.com/404.php & ErrorDocument 404 /404.php
Pls let me know if anyone have solution
ErrorDocument 404 http://jobguide.australianenglishcenter.com/404/
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/404/$
RewriteRule ^(.*)$ <YourRelativePathToPHPFile>/404.php [L]
Here,The ErrorDocument redirects all 404s to a specific URL
The Rewrite rules map that URL to your actual 404.php script. The RewriteCond regular expressions can be made more generic if you want, but I think you have to explicitly define all ErrorDocument codes you want to override.

Htaccess RewriteRule redirects to 404 page

My basic requirement is:
Remove .php extensions from all the urls.
Rewrite the urls from http://localhost/unsync/softwares/page_name/sub_category/ to http://localhost/unsync/softwares.php?p=page_name&sub_cat=sub_category
The following is my .htaccess code:
# Do not remove this line, otherwise mod_rewrite rules will stop working
Options +MultiViews
RewriteEngine On
RewriteBase /
#Prevent viewing of .htaccess file
<Files .htaccess>
order allow,deny
deny from all
</Files>
#Prevent directory listings
Options All -Indexes
#Error Documents
ErrorDocument 400 /unsync/error.php?code=400
ErrorDocument 401 /unsync/error.php?code=401
ErrorDocument 402 /unsync/error.php?code=402
ErrorDocument 403 /unsync/error.php?code=403
ErrorDocument 404 /unsync/error.php?code=404
ErrorDocument 500 /unsync/error.php?code=500
ErrorDocument 503 /unsync/error.php?code=503
#Remove extensions
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ /unsync/$1.php [NC,L]
RewriteRule softwares/(.*)/(.*)/$ /softwares.php?p=$1&sub_cat=$2 [L]
DirectoryIndex index.php
The problem I am facing is that, RewriteRule fails. I mean, when I try to access softwares/page_name/sub_category, its throwing a 404 error.
Note: Its removing the .php extensions properly and working fine with normal pages.
The problem is that your rewrite rule rewrites all requests to /unsync/request.php, you have to check the existance of php file before rewriting the request,
#Remove extensions
RewriteCond %{DOCUMENT_ROOT}/unsync/$1.php -f
RewriteRule ^([^\.]+)$ /unsync/$1.php [NC,L]
Or you can simply exclude the slash in pattern so that it can not conflict with your other rule
RewriteRule ^([^\/.]+)$ /unsync/$1.php [NC,L]
Rewrite rules are tried in order.
This means softwares/page_name/sub_category is rewritten by the first rule to /unsync/softwares/page_name/sub_category.php, which is not found. Therefore, you get a 404 Not found error.
After a long day of research, I could finally resolve my issue on my own as follow (if anyone facing similar issue is searching for solution):
#If both p and sub_cat are issued
RewriteRule ^softwares/(.+)/(.*)$ /unsync/softwares.php?p=$1&sub_cat=$2 [NC,L]
#If only p is issued
RewriteRule ^softwares/(.*)$ /unsync/softwares.php?p=$1 [NC,L]
#Remove extensions
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ /unsync/$1.php [NC,L]
DirectoryIndex index.php

Finding the php file which is executed (the name is hidden by .htaccess)

I've a very weird situation on my own website.
When I try to execute this page:
http://www.****.com/Militari/Guida-scelta-.htm
Or this page
http://www.****.com/Militari/Guida-.htm
I get the same result. Looking to the .htaccess/PHP files it's a completely unpredictable result.
It looks like if the executed php file would make a DB search and try to find the best matching result.
This is on a shared hosting.
How can I find which PHP file is being executed?
The .htaccess
RewriteEngine on
RewriteRule ^Cinema/Interviste/([^/]*)\.htm$ /Cinema/Interviste/film.php?title=$1
RewriteRule ^Cinema/dvd/([^/]*)\.htm$ /Cinema/dvd/film.php?title=$1
RewriteRule ^Cinema/([^/]*)\.htm$ /Cinema/film.php?title=$1
RewriteRule ^Attori/(.*)\.htm$ Attore.php?name=$1
RewriteRule ^Registi/(.*)\.htm$ Regista.php?regia=$1
RewriteRule ^(.*)\.htm $1\.php
RewriteRule filmrecensiti\.xml filmrecensiti\.xml\.php
RewriteRule filmrecensiti2\.xml filmrecensiti2_old\.xml
RewriteRule ^Elenco\sFilm\s(.)\.php Elenco_Film_Recensiti\.php?starting_letter=$1
RewriteRule ^Elenco\sFilm\sRecensiti(.*) Elenco_Film_Recensiti$1
RewriteRule ^(.*)\.js php_js\.php\?id=$1\.js
RewriteRule ^(.*)\.css php_css\.php\?id=$1\.css
#RewriteRule ^feeds/(.+)$ feeds_items/$1 [L]
ErrorDocument 400 /not_found.htm
ErrorDocument 401 /not_found.htm
ErrorDocument 403 /not_found.htm
ErrorDocument 404 /not_found.htm
ErrorDocument 500 /not_found.htm
EDIT
I had missed the .htaccess in the subfolder
its content is
RewriteRule ^([^/]*)\.htm$ film.php?title=$1
I found the problem thanks to your advice.
This helped a lot: $_SERVER['PHP_SELF'] or $_SERVER['SCRIPT_NAME']
From what you've posted, the only applicable rule would be RewriteRule ^(.*)\.htm $1\.php suggesting that http://www.****.com/Militari/Guida-scelta-.htm would actually serve up http://www.****.com/Militari/Guida-scelta-.php.

Folder Not Raising 404 Error In htaccess

Simply, I am trying to make it so if a user goes to a folder, it will give a 404 Error. Problem is, it does not seem to work!
Here is my file setup:
- postin
- mail
- yourmail.php
- profiles
- viewprofile.php
- .htaccess
- error.php
Then simply I am adding this to my .htaccess:
RewriteRule ^mail/ - [L,R=404]
ErrorDocument 404 error.php
I thought by adding this code that if a user typed in the URL .../postin/mail/ that it would take the user to the 404 error.php page. That does not seem the be the case though, it will just bring up the directory page for .../postin/mail/. So my question is, how do I raise the 404 error when a user goes to a folder (directory)?
If you would like my full .htaccess it is here:
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 search/(.+)/?$ search.php?query=$1 [NC,L]
RewriteRule search/?$ search.php [NC,L]
RewriteRule register/?$ registerpage.php [NC,L]
RewriteRule login/?$ loginpage.php [NC,L]
RewriteRule home/?$ home.php [NC,L]
RewriteRule users/?$ users.php [NC,L]
RewriteRule error/?$ error.php [NC,L]
RewriteRule ^mail/ - [L,R=404]
ErrorDocument 400 http://localhost/postin/error
ErrorDocument 401 http://localhost/postin/error
ErrorDocument 403 http://localhost/postin/error
ErrorDocument 404 http://localhost/postin/error
ErrorDocument 500 http://localhost/postin/error
ErrorDocument 502 http://localhost/postin/error
ErrorDocument 504 http://localhost/postin/error
<Files .htaccess>
order allow,deny
deny from all
</Files>
You can use :
DirectoryIndex /errorpage.php
This should be the ver first line in your htaccess

Can't access website's root page

Usually, going to my website, qpcftw.cu.cc would load the page qpcftw.cu.cc/index.php transparently, without the /index.php part showing in the url. I've been trying to get rid of the .php extensions using .htaccess. This is the contents of my .htaccess file:
<Files />
ForceType application/x-httpd-php
</Files>
RewriteEngine on
RewriteBase /
RewriteRule ^.htaccess$ - [F]
RewriteRule ^([A-z,0-9,_,-]+)?$ $1.php [QSA]
RewriteRule ^([A-z,0-9,_,-]+)/index\.html$ $1.php [QSA]
ErrorDocument 403 php/error.php
ErrorDocument 404 /404.php
ErrorDocument 405 php/error.php
ErrorDocument 408 php/error.php
ErrorDocument 500 php/error.php
ErrorDocument 502 php/error.php
ErrorDocument 504 php/error.php
I need to meet 3 criteria:
qpcftw.cu.cc/index.php == qpcftw.cu.cc/index
Visiting qpcftw.cu.cc/forum/ still loads my PHPBB forum at qpcftw.cu.cc/forum/index.php
qpcftw.cu.cc loads the index.php file transparently, without it shown in the URL
So far my current .htaccess fulfills the first 2 needs, but breaks the 3rd. Help! :/
The following .htaccess code automatically does all the extension hiding magic:
Options +MultiViews
Instead of blindly adding .php to everything, check if the .php file exists first
RewriteCond $1.php -f
RewriteRule ^([A-z0-9_-]+)?$ $1.php [L,QSA]
RewriteCond $1.php -f
RewriteRule ^([A-z0-9_-]+)/index\.html$ $1.php [L,QSA]
This still works for 1 and 2, and 3 now also works because the code will test if index.php.php exists (the request is for index.php), which it doesn't (I hope!), so the RewriteRule will fail.
I've also removed the commas from the regex classes (you can't use those to indicate several groups, putting it in there just means commas are allowed in URLs), and added the [L]ast flag to the rules for better performance

Categories