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
Related
There is an issue with redirection to error pages:
example.com/test - will redirect to 404 error page
but
example.com/test/ - will go to the white "File not found." page
to mention:
it was working properly until some time ago (maybe update of PHP version ??)
same behavior with www/http/https version of the links
standard structure of the links is www.example.com/test/
.htaccess file code
<Files .htaccess>
order allow,deny
deny from all
</Files>
RewriteEngine On
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteRule sample/(.*)/(.*)/$ /sample.php?$1=$2
ErrorDocument 400 /400.php
ErrorDocument 401 /401.php
ErrorDocument 403 /403.php
ErrorDocument 404 /404.php
ErrorDocument 410 /410.php
The difference with your URLs that end in a trailing slash is that they are unconditionally rewritten to the corresponding .php file. The URLs that do not end in a trailing slash are not rewritten - nothing happens.
You are seeing the same basic "File not found" response when you directly request a non-existent .php file, regardless of whether the request is rewritten (by your rules) or not.
The "problem" might be due to the way PHP is implemented on your server. For instance, if all *.php requests are proxied to an alternative backend process then this is going to bypass your .htaccess file on the application server and the "basic" 404 response you are seeing is possibly coming from the proxy, not your application server.
You may be able to resolve this by first checking that the .php exists before rewriting to it (so it doesn't trigger a 404). And if none of your URLs contain a .php extension, you could also force any direct request for .php files to 404 (on your server, before the request is proxied - if that is what's happening).
RewriteEngine On
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteRule sample/(.*)/(.*)/$ /sample.php?$1=$2
The first two rules can also be combined into one. You are missing L flags on all your rules. You need to ensure that MultiViews is disabled, otherwise the last rule will not work.
Also, the regex in the last rule needs to be anchored and made more specific since it is matching too much, eg. /sample/foo/bar/baz/qux will be rewritten to /sample.php?foo/bar/baz=qux, which I assume is not the intention.
Try the following instead:
Options -MultiViews
RewriteEngine On
# Force any direct request for ".php" files to 404
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \.php$ - [R=404]
# Rewrite to ".php" file - 1 or 2 path segments
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^/]+(/[^/]+)?)/$ $1.php [L]
# Rewrite "/sample/one/two/"
RewriteRule ^sample/([^/]+)/([^/]+)/$ sample.php?$1=$2 [L]
Reference:
Another recent question that has a very similar issue and was resolved in the same way:
Custom 404 error handler in htaccess not working for non-existent ".php" files
The problem is with ending slash of RewriteRule ^([^/]+)/$ $1.php
If you write RewriteRule ^([^/]+)/?$ $1.php the tailing slash would be optional.
edit
You should also add
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
before RewriteRule statements, because of server loop - when the file exists the statements will break loop by skipping rewriting.
Hey so I'm trying to get my htaccess file to do 2 things:
1) if user enters http://dstealth.com/i or http://dstealth.com/i/some/other/address I want htaccess file to use http://dstealth.com/z_index/ or http://dstealth.com/z_index/some/other/address
2) I also want the htaccess file to redirect the user if they use the old address. eg if user types http://dstealth.com/z_index or http://dstealth.com/z_index/some/other/address I want the URL to appear as http://dstealth.com/i/some/other/address
Basically at all times replace z_index with i even though the directory i does not exist and z_index is still the directory being used.
I've tried a bunch of stuff and it seems not to be working as it keep redirecting me to my 404 page.
My .htaccess file:
ServerSignature Off
ErrorDocument 403 /error_pages/403.php
ErrorDocument 404 /error_pages/404.php
ErrorDocument 500 /error_pages/500.php
IndexIgnore *
RewriteEngine On # Turn on the rewriting engine - only needed once
Options -MultiViews
# Prevent directory listings
Options All -Indexes
# Prevent viewing of .htaccess file
<Files .htaccess>
order allow,deny
deny from all
</Files>
# Rewrite www.dstealth.com as dstealth.com
RewriteCond %{HTTP_HOST} ^www\.dstealth\.com [NC]
RewriteRule ^(.*)$ http://dstealth.com/$1 [R=301,NC]
# Rewrite /z_index/ as /i/
RewriteRule ^z_index/([^/.]+)$ /i/$1 [NC]
RewriteRule ^i/([^/.]+)$ /z_index/$1 [NC,L]
results:
http://dstealth.com/z_index/ still appears exactly the same and doesnt change to http://dstealth.com/i/
http://dstealth.com/i/ takes me to my 404 page :(
I believe I got what I want with the following code. Please let me know if it has any errors or inefficiencies:
RewriteCond %{THE_REQUEST} \s/z_index [NC]
RewriteRule ^z_index/([^.]+)\.php$ /i/$1\.php [R=302,L]
RewriteCond %{THE_REQUEST} \s/z_index [NC]
RewriteRule ^z_index/([^.]+)$ /i/$1 [R=302,L]
RewriteCond %{THE_REQUEST} \s/z_index [NC]
RewriteRule ^z_index/?$ /i/ [R=302,L]
RewriteRule ^i/([^.]+)\.php$ /z_index/$1\.php [NC,L]
RewriteRule ^i/$ /z_index/?&%{QUERY_STRING} [NC,L]
I need to rewrite the following url:
http://localhost/homemarket/products/C1/C2/
to
http://localhost/homemarket/products.php?catergory=C1&sub_category=C2
I tried searching all over stackoverflow, and found similar rewrite rules but I am facing the following problems:
These rules clash with my removing .php rules (adds .php to my sub_category query).
If subcategory is removed, its redirecting to 404 page.
If none of them are provided, its again redirecting to 404 page.
Here's what I have tried:
# 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 404 /homemarket/error.php?404
ErrorDocument 500 /homemarket/error.php?500
#Remove extensions
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ /homemarket/buyers/$1.php [NC,L]
RewriteRule ^products/([^/]*)/([^/]*)/?$ /homemarket/buyers/products.php?category=$1&sub_category=$2 [NC,L]
DirectoryIndex index.php
Your first rule matches both uris, You need to exclude the slash in your rule so that it can not conflict with other rules :
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/.]+)$ /homemarket/buyers/$1.php [NC,L]
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
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