I am trying to use .htaccess file for my REST service but I found that the url are not actually redirected. The web server show
I wanted to use url such as
/api/v1/test
instead of
/api/v1/rest.php?request=test
I have already check mod_rewrite is acutally working.
My .htaccess file is as below
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule api/v1/(.*)$ api/v1/rest.php?request=$1 [QSA,NC,L]
I have already look at PHP redirecting problem and Sitemap redirecting
you can try this rule instead because it works with me :
RewriteRule api/v1/([A-Za-z0-9-]+)/?$ api/v1/rest.php?request=$1
try putting .htaccess file in htdocs directory and if the directory api location is
C:\xampp\htdcos\xampp\www\api
then put the full path in the rule
this site generates mod_rewrite for you:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^api/v1/([^/]*)$ /api/v1/rest.php?request=$1 [L]
Related
I uploaded the code to the server. It started the homepage correctly . But when I press any link , I get 404 not found error. I discovered that I need to add index.php to my url for it to work.
so it will be like that:
mydomain.somee.com/myWebsite/index.php/anotherPage
When I was working locally using Xamp as a server, I didn't get any of those problems.
I got those problems after I uploaded the website to some.com which apparently doesn't use .htaccess file (editing or removing has no effect).
How to add this index.php automatically and hide it from the user?
I didn't change any of the system files or the htaccess
please tell me if you need anymore files or description.
You need to redirect all your all pages through index.php file but remove it from URL.
Write below rules in your root .htaccess file:-
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
OR
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ %1 [R=301,L]
To understand, How htaccess rules are working, This link will help you :)
Hope it will help you :)
I have the following code on my htaccess:
RewriteEngine On
RewriteRule ^course-details/([a-zA-Z0-9]+)/$ course-details.php?id=$1
I think this gave me a URL like this: /cp/course-details/5 where 5 is the id of the course.
But when I go to this address I get 404 not found. Also, our current url is
/cp/course-details.php?course-details.php?name=bla-bla-bla&category_id=1
and we need a friendly url like this
cp/category-name/course-name/
Thanks in advance for you help.
You can try this in your htaccess file if the php file is in the cp dir. Since it appears your are developing in a subfolder(peppers) of your dev box you can try this.
RewriteEngine On
RewriteBase /peppers/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^cp/course-details/([^/]+)/?$ cp/course-details.php?name=$1 [NC,L]
If this is for production then you can use this in the .htaccess file in the root.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^cp/course-details/([^/]+)/?$ /cp/course-details.php?name=$1 [NC,L]
RewriteRule ^course-details/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/.*$ course-details.php?id=$1&category_id=$2
With that, you can use URL like course-details/1/5/category-name/course-name
Because URL allow: course-details/category_id/id/what-ever-you-want
I would like to find a RewriteRule that does this :
mysite.net/jqMAS/ or mysite.net/jqMAS => mysite.net/index.php?id=jqMAS
I used such a .htaccess file :
RewriteEngine On
RewriteRule ^(.*) index.php?id=$1
But unfortunately, it doesn't work (maybe mysite.net/index.php is itself redirected to mysite.net/index.php?index.php, etc. ?) : calling mysite.net/jqMAS produces a 500 Internal Server Error.
What RewriteRule should we use to do such URL shortening ?
Here is what the index.php page (I didn't mention the headers) looks like :
<body>
Bonjour <?php echo $_GET['id']; ?>
</body>
Try the following your .htaccess file, as it is the setup successfully used on my own website.
# Enable the rewriting engine.
RewriteEngine On
# Change requests for a shorter URL
# Requires one or more characters to follow the domain name to be redirected
RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?id=$1 [L]
There are 2 htaccess files:
Keep your htaccess file within application folder as:
Deny from all.
and paste following code to your htaccess file outside the application folder:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
It is working perfect for me.
You need RewriteCond to stop rewriting for real files and directories:
RewriteEngine On
# if request is not for a directory
RewriteCond %{REQUEST_FILENAME} !-d
# if request is not for a file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?id=$1 [L,QSA]
This is my first question:
I have created a URL Shortner Service in PHP.It works completely without any problem but there was a problem:
Someone who wants to access his url should type : MyDomain.com/go.php?u=key
Here i tried to remove .php extention by configuring apache and worked.Now it is like that:
MyDomain.com/go?u=key
but some services such TinyUrl.com works like that: TinyURL.com/key !!!!
How can i get this in php?
thanks a lot.
You basicly use mod_rewrite.
With mod_rewrite you can say that all requests which are like
www.example.com/[A-Za-z1-9]
are redirected to:
www.example.com/shorturl.php?key=$1
While $1 is the extracted variable from the requested URL.
There is no proper way to do it with pure PHP.
The rewrite rule could look like this:
RewriteRule ^([A-Za-z1-9]*)$ shorturl.php?key=$1 [L]
I would exclude files which really exists from the rewriting, use for this
RewriteCond.
This could be done like here:
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
# If the request is not for a valid link
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([A-Za-z1-9]*)$ shorturl.php?key=$1 [L]
(Source: anubhava at RewriteCond to skip rule if file or directory exists)
create .htaccess file to redirect any request to your domain to one file lets say go.php
so in .htacess do like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ go.php?key=$1 [PT,L]
</IfModule>
This redirection any request like mydomain.com/userkey to mydomain.com/go.php?key=userkey
now in your index.php you can do your redirection login.
<?php
$key = $_GET['key'];
// your logic here.
?>
This ref will solve your problem.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9]+)/?$ redirect.php?c=$1 [L]
</IfModule>
I have a rewrite rule in my .htaccess.
RewriteRule ^(.+?)$ /user_home.php?domain=$1 [L,QSA]
Which redirects mysite.com/mypage to mysite.com/user_home.php?domain=mypage
This works fine on my shared host, but when working locally, my site is located in www/mysite/ and instead of redirecting to www/mysite/user_home.php?domain=mypage it tries to redirect to www/user_home.php?domain=mypage which doesn't exist.
I get the following error in my Apache Logs
[error] [client ::1] script 'C:/wamp/www/user_home.php' not found or unable to stat
How can I alter my .htaccess to make it direct to the file in the current directory using a relative path instead of an absolute path..
You just need to remove leading slash from the target URL in your rule like this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)/?$ user_home.php?domain=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f is also needed to prevent infinite looping.
Try this code :
RewriteBase /mysite/
RewriteRule ^(.+?)$ /user_home.php?domain=$1 [L,QSA]
You just have to add a RewriteBase with the path of the root folder of your website.
The best solution would be to add checks if there is a file called like this and set a RewriteBase:
RewriteEngine On
RewriteBase mysite/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+?)$ /user_home.php?domain=$1 [QSA,L]