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]
Related
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 have a file called car-details.php it takes the variable id= with the cars registration details in order to populate the page.
I can currently get this rewrite rule working
RewriteRule ^car-for-sale/(.*) car-details.php?id=$1 [NC,L]
But I would like to have a url like
/GV09LBX/Ford-car-something-etc
but for some reason I just can't get the second part of the url to work. I followed some guides in order to add a second part to the url example below
RewriteRule ^car-for-sale/(.*)/(.*) car-details.php?id=$1&something=$2 [NC,L]
Where something=$2 isn't even used by the car-details.php file but I would just like to have the car name in the url for seo purposes. Whenever I've tried to add a second forward slash the rewrite rule stops working. Any help?
full htaccess file below
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^car-for-sale/(.*)/(.*) car-details.php?id=$1&something=$2 [NC,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
# Wordfence WAF
<IfModule LiteSpeed>
php_value auto_prepend_file '/home/shipley/public_html/wordfence-waf.php'
</IfModule>
<Files ".user.ini">
<IfModule mod_authz_core.c>
Require all denied
</IfModule>
<IfModule !mod_authz_core.c>
Order deny,allow
Deny from all
</IfModule>
</Files>
You must use anchor in your patterns:
RewriteRule ^car-for-sale/([^/]+)/([^/]+)/?$ car-details.php?id=$1&something=$2 [NC,L,QSA]
RewriteRule ^car-for-sale/([^/]+)/?$ car-details.php?id=$1 [NC,L,QSA]
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
I am building a wiki using dokuwiki, and am using the nice URL feature which uses url rewriting through Apache on the webserver, to get rid of the ugly php urls. My problem is that I seem to be only getting 404 Not Found errors when searching for the pages using the new URLs.
This is the .htaccess file provided, and the RewriteBase is pointed to the correct location.
#Options -Indexes -MultiViews +FollowSymLinks
## make sure nobody gets the htaccess, README, COPYING or VERSION files
<Files ~ "^([\._]ht|README$|VERSION$|COPYING$)">
Order allow,deny
Deny from all
</Files>
## Uncomment these rules if you want to have nice URLs using
## $conf['userewrite'] = 1 - not needed for rewrite mode 2
RewriteEngine on
RewriteRule ^_media/(.*) lib/exe/fetch.php?media=$1 [QSA,L]
RewriteRule ^_detail/(.*) lib/exe/detail.php?media=$1 [QSA,L]
RewriteRule ^_export/([^/]+)/(.*) doku.php?do=export_$1&id=$2 [QSA,L]
RewriteRule ^$ doku.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) doku.php?id=$1 [QSA,L]
RewriteRule ^index.php$ doku.php
## Not all installations will require the following line. If you do,
## change "/dokuwiki" to the path to your dokuwiki directory relative
## to your document root.
RewriteBase /dokuwiki
## If you enable DokuWikis XML-RPC interface, you should consider to
## restrict access to it over HTTPS only! Uncomment the following two
## rules if your server setup allows HTTPS.
#RewriteCond %{HTTPS} !=on
#RewriteRule ^lib/exe/xmlrpc.php$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R=301]
Make sure your Apache actually loads .htaccess files. You will probably need this in your Apache config:
AllowOverride All
So I have been looking all over for a while, and cannot seem to find a particular solution I am looking for. I have seen many articles showing how to fix problems with wordpress rewrite, but none fix my problem.
I currently have Wordpress Installed in my / Root directory. the .htaccess looks like this.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^manager - [L,NC] <-- I added this from other examples.
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
So from what I can tell this tells wordpress, hey you will handle all the urls, for everything. forever. Seriously. For ever and everywhere :P
So the problem comes when I have a sub directory site.com/manager
Here I would like to build a similar URL capturing method to manage what the user gets to see/access/etc. The line I added to the root .htaccess file (from what I have read) is suppose to tell wordpress, hey don't touch my damn /manager folder.
Wordpress being the sneaky greedy caregiver that it is fails to listen. ill go to test my site.com/manager/test (grabbing test and pulling info from sql with it) and wordpress takes over and throws me to the wordpress 404 page.
my .htaccess in /manager is as follows for refrence, I am thinking I will need to do some rewrite stuffs here too, but for now just trying to prevent wordpress from being over bearing :).
DirectoryIndex index.php
<FilesMatch "\.(php|inc)$">
Order allow,deny
deny from all
</FilesMatch>
<FilesMatch "(index.php|logout.php)$">
Order allow,deny
allow from all
</FilesMatch>
Any ideas, thoughts, comments, concerns would be much appreciated.
Thank you.
SO after digging deeper and looking into this further I found a fix. Not sure it is the BEST solution but its working.
Root (/) .htaccess
#Manager - A customer content manager
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/manager/(.*)$ [OR]
RewriteRule ^.*$ - [L]
</IfModule>
#end Manager
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
This appears to over ride wordpress like needed.
Manager (/manager) .htaccess
DirectoryIndex index.php
<FilesMatch "\.(php|inc)$">
Order allow,deny
deny from all
</FilesMatch>
<FilesMatch "(index.php|logout.php|install.php)$">
Order allow,deny
allow from all
</FilesMatch>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /manager/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>
The top portion isn't required but I added this for file protection/security.
Summary:
I used the %{REQUEST_URI} to selectively remove a sub-folder, to apply this to more sub-folders I would add another
RewriteCond %{REQUEST_URI} ^/manager/(.*)$ [OR]
I then input the information in the sub-directory's .htaccess file for url rewriting. From here I can manipulate the URL converting it into a Array list for further sorting and database calling.