htaccess rewrite rule in subfolder - php

Hi i'm trying to create a REST API method in core php
I created a controller class and method class to call my function as per the reference
http://phppot.com/php/php-restful-web-service/
I have my files in sub-folder in my server where i'm currently creating this i face issue on accessing rewrite URL , how can u solve this issue below is the htaccess rule which i tried.
RewriteEngine on
RewriteBase /test/rest/
# map neat URL to internal URL
RewriteRule ^/test/rest/mobile/list/$ /test/rest/RestController.php?view=all [nc,qsa]
RewriteRule ^/test/rest/mobile/list/([0-9]+)/$ /test/rest/RestController.php?view=single&id=$1 [nc,qsa]

The problem is with the pattern, because there is no leading slash, see Per-directory Rewrites
Per-directory Rewrites
...
The removed prefix always ends with a slash, meaning the matching occurs against a string which never has a leading slash. Therefore, a Pattern with ^/ never matches in per-directory context.
So to fix this remove the leading slash in your patterns
RewriteRule ^test/rest/mobile/list/$ /test/rest/RestController.php?view=all [NC,QSA]
RewriteRule ^test/rest/mobile/list/([0-9]+)/$ /test/rest/RestController.php?view=single&id=$1 [NC,QSA]
Unrelated, but you don't need RewriteBase in this case, because you already use absolute substitution URLs.
Also, QSA|qsappend is only needed, if you expect that the requests have a query string.

try the below code
RewriteEngine on
RewriteBase /test/rest/
RewriteRule /(.*)/ RestController.php?view=$1
RewriteRule /(.*)/(.*)/ RestController.php?view=$1&id=$2

I got solution using Rewriting URL tool
http://www.webconfs.com/web-tools/url-rewriting-tool/

Related

Using htaccess modrewrite for friendly urls gives a 404 error

I am trying to rewrite
http://example.com/category/this-is-my-category
to...
http://example.com/category.php?id=this-is-my-category
My .htaccess file is below:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^category/(\d+)/([\w-]+)$ /category.php?id=$1 [L]
This gives a 404 error
http://example.com/category.php exists on the server
I have also tried
RewriteRule ^category/(\d+)/([\w-]+)$ ./category.php?id=$1 [L]
and
RewriteRule ^/category/([a-zA-Z0-9]+)$ /category.php?id=$1
I have read some articles on this and can't see an issue with the code in the .htaccess file.
Right, so your first regex...
^category/(\d+)/([\w-]+)$
requires a number between category and the last part, eg /category/1234/something-else.
Your second regex...
^/category/([a-zA-Z0-9]+)$
has an incorrect leading slash (rewrite rules start at the rewrite-base) and requires only letters and numbers after category, eg /category/thisIsMyCategory.
The URL you're testing has letters and hyphens.
To me, it looks like you want
RewriteEngine On
RewriteRule ^category/([\w-]+)$ /category.php?id=$1 [L,QSA]
Demo ~ https://htaccess.madewithlove.be?share=8c3de5aa-68f3-5ec0-9c69-23ff2dbe2d6e
Some notes...
It's rare to ever need RewriteBase, especially if your .htaccess file is in the root directory so I've removed it
I've added the QSA flag so any query parameters are preserved. For example
/category/this-is-my-category?foo=bar
becomes
/category.php?id=this-is-my-category&foo=bar

.htaccess redirect and rewrite url if REQUEST_URI starts with specific path

I am working on a Wordpress site, it requires a plugin that needs to access the API endpoint of WP.
It wants to access the site's:
http://myhost.test/wp/wp-json/erp/v1/hrm/employees/1\?include\=department,designation,reporting_to,avatar,roles
but changes to the folder structure, causes the real, and actual link should be:
http://myhost.test/wp-json/erp/v1/hrm/employees/1\?include\=department,designation,reporting_to,avatar,roles
as you can see, there should be no /wp at the start of the %{REQUEST_URI}
I am trying to make this work: but it won't redirect the request:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^/wp/wp\-json/
RewriteRule ^/wp/wp\-json(.*) /wp\-json$1 [L,R]
I do not understand what I am doing wrong,
I am catching everything after ^/wp/wp-json then forward it to ^/wp-json
What is going on?
Regards,
Using mod_rewrite in .htaccess context vs a <Directory> or <VirtualHost> context each have slightly different requirements in how the patterns are parsed. Most importantly to your situation, in .htaccess the first argument to RewriteRule is not matched against a leading slash / because the pattern is considered relative to the directory it is in.
Remove the leading / from your RewriteRule matcher argument:
RewriteRule ^wp/wp\-json(.*) /wp\-json$1 [L,R]

Removing duplicate slashes in URL via .htaccess or PHP

I'm trying to remove duplicate slashes from URLs. The following .htaccess rule:
RewriteRule ^(.+)//+(.*)$ $1/$2 [L,NC,R=301]
do NOT work for me on a URL such as the following:
http://www.mp7.org/?site=69.com\\\\\\\\\\\\\\\\
The .htaccess file
#### mod_rewrite in use
Options +FollowSymlinks
RewriteEngine On
This rule won't work for backslashes. You must add a similar rule with backslashes
RewriteRule ^(.+)\\\\+(.*)$ $1\\$2 [L,R]
If you want to replace backslashes with (forward) slashes, use this rule instead
RewriteRule ^(.+)\\\\+(.*)$ $1/$2 [L,R]
And to remove all back-/slashes at the end of the request
RewriteRule ^(.*?)[/\\]+$ $1 [L,R]
and the same when it is a query string
RewriteCond %{QUERY_STRING} ^(.*=.*?)[/\\]+$
RewriteRule ^.*$ $0?%1 [R,L,QSA]
When everything works as you expect, you can change R to R=301.
Never test with 301 enabled, see this answer
Tips for debugging .htaccess rewrite rules
for details.
This is because of IIS server, and mainly almost certainly your site is urbanized in .Net .asp or . aspx You need to put into service one miniature script, when your server be given http or https demand it must Lcase(URL). If there are not a lot of pages, you can rename all within lowercase, modify all inner linking of WebPages to lowercase and after that appeal URL removal request in Google webmaster tools.
how to remove duplicate urls

URL Rewrite rule in .htaccess for Root directory

I am using this rule to rewrite the link
RewriteEngine On
RewriteRule ^(.*)/(.*) show_cv.php?email=$1
It is working fine like if I write this url with last slash
www.mysite.com/letschat_2008#yahoo.com/ ----> index.php?email=letschat_2008#yahoo.com
But when I remove the last slash from the link www.mysite.com/letschat_2008#yahoo.com/ it shows error 404.
I wish the URL Rewrite rule would work for both with slash and without slash (/)
www.mysite.com/letschat_2008#yahoo.com/ ----> index.php?email=letschat_2008#yahoo.com
www.mysite.com/letschat_2008#yahoo.com ----> index.php?email=letschat_2008#yahoo.com
Your rules are looping, you need to make sure you are rewriting an email address, and add some conditions so that the rule doesn't get applied if it's accessing an existing resource:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9_\-\#\.]+)/?$ /show_cv.php?email=$1 [L]
You should then be using the following rule:
RewriteRule ^(.*)/? show_cv.php?email=$1
I assume you note these rules in a .htaccess file, not in the server configuration when looking at your description ?
Rethink if you don-t want to put this into the server configuration. Apart from the usage of .htaccess files being harder to debug using rewrite rules in those files is more complex than in the server configuration. This is documented in mod_rewrites docs.
The reason for the behaviour is the different content of REQUEST_URI in both cases. Have a try checking this directly and you will see the problem. The whole part "letschat_2008#yahoo.com" is simply missing in that variable in case 2 (no "/"). To get this working you must use an additional rewriteCondition (also documented...). Something like these untested lines:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/(.+)&
RewriteRule - show_cv.php?email=%1
(note the '%' instead of a '$' in the last line)

.htaccess ModReWrite help

I am having some trouble with my ReWrite code. Please note that the .htaccess file is in the subdomain folder (...public_html/subdomain/ )
I am simply trying to rewrite a page request:
http://subdomain.mysite.com/home
http://subdomain.mysite.com/index.php?page=home
My .htaccess file looks like this...
RewriteEngine On
RewriteRule ^/([A-Za-z0-9\-\_])$ /index.php?page=$1
Does anything jump out at you?
Your current rule probably works for urls one character long (after the slash)!
Add a + to signify one or more characters, or a * for zero or more
Try
RewriteEngine On
RewriteRule ^/([A-Za-z0-9\-\_]*)$ /index.php?page=$1
If you want to use the rules in a .htaccess file, you need to strip the contextual per-directory path prefix from the RewriteRule pattern. If the .htaccess file is located in the document root /, you need to strip the leading /.
Additionally you need to quantify the character set. Otherwise it would only describe one character.
So try this rule:
RewriteRule ^([A-Za-z0-9-_]+)$ index.php?page=$1
I think
RewriteRule ^([^/]*)$ /index.php?page=$1 [L]
is ok ;)

Categories