Rewrite Specific PHP file to directory with htaccess - php

So I have a URL that is domain.com/s_cart.php?&pid=1 and /s_cart.php?&gid=2
How would i go about setting a .htaccess to rewrite s_cart.php to the following
domain.com/order/&pid=1
Tried the following
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/order([/]*)$ ./s_cart.php?gid=5 [L,NC]
Didn't work.
Basically I'm trying to add domain.com/order?pid=1
When the actual url currently is domain.com/s_cart.php?pid=1

Related

remove 3 directories in url via htaccess file

I want to convert the URL http://localhost/project1/mvc/public/content/1 to the URL http://localhost/content/1.
I have my .htaccess file placed in the public folder that removes the index.php in the URL.
Options -MultiViews
RewriteEngine On
RewriteBase /project1/mvc/public
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
Here's my directory structure:
Project 1
---mvc
-----app
-----public
I need help in the creating .htaccess file for this. What is the syntax for modifying the URL and which folder/s should I put the .htaccess file/s?
Thanks
You can use this code in your DOCUMENT_ROOT/.htaccess file (above a directory level of mvc):
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^((?!/project1/mvc/public/).*) project1/mvc/public/$1 [L,NC]

php friendy url using htaccess

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

Need url mod rewrite conditions in .htaccess file

I'm having trouble getting my .htaccess rewrites to work:
1.
http://www.sunfolks.com/ittraining.phpabove link is working fine but i want seo friendly url.it Should be: http://www.sunfolks.com/ittraining
2.
http://www.sunfolks.com/ittraining02.php?onlinetraining=java-online-training-in-usaabove link is working fine but i want seo friendly url.it Should be: http://www.sunfolks.com/ittrainings/onlinetraining/java-online-training-in-usa
What are all the changes i have to do for this?
Here's my .htaccess file:
RewriteEngine On
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L,QSA]
RewriteRule ^ittrainings/code/([0-9]+)$ ittraining02.php?code=$1 [L]
</IfModule>
You need to explain better next time and provide what you have done that is not working in full details, with some sample outputs.
Why not use something like this and see if it works:
Options +FollowSymlinks
# Turn on URL rewriting
RewriteEngine On
# Installation directory
RewriteBase /
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/
RewriteRule .* index.php?url=$0 [PT,L]
Then have a index.php file that can process the incoming requests using $_GET['url'].
e.g:
$url_args = explode("/", $_GET['url']);
$section = $url_args[0];
$category = $url_args[1];
$data1 = $url_args[2];
Then use the variables appropriately in the remainder of your code.

Request param from url in mvc using htaccess and php

The real url format is
http://localhost/myfolder/index.php?url=login/register&step=1
After mod rewrite I get
http://localhost/myfolder/login/register/step/1
All good but how can I get the value of step in a php file.If I do $_GET['step'] then I get nothing.
My htaccess
Options -MultiViews
# turn rewriting on
RewriteEngine On
# When using the script within a sub-folder, put this path here, like /mysubfolder/
# If your app is in the root of your web folder, then please delete this line or comment it out
RewriteBase /mybb/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)/(.*) index.php?url=$0&step=$2 [L,QSA]
Change your rule to:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)(?:/(step)/([0-9]+))?/?$ index.php?url=$1&$2=$3 [NC,L,QSA]

.htaccess rewrite to index.php infinite loop

I am building a clean url system using .htaccess and php.
The main idea is to rewrite everything to index.php and to split the url to segments separated by /.
It works but when I upload it to web server I get an infinite loop error.
My .htaccess file is:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
If I get things correctly - the problem is that it redirects index.php to itself but it shouldn't because I have index.php file that handles everything and RewriteCond %{REQUEST_FILENAME} !-f which should prevent that rewrite.
Could you please help me with this .htaccess - I want it to rewrite everything to index.php and to be as portable as it can be, so I can use it in:
www.mysite.com
www.myothersite.com
or even
www.mysite.com/new/
I also want to redirect something like www.mysite.com/articles/2 to index.php as well as www.mysite.com/articles/it/2 or even www.mysite.com/articles/it/search/searchterm
Here's code taken from Drupal's .htaccess which uses the "clean urls" approach you are trying to achieve.
# Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
In this case, it appends the clean URL as the "q" querystring parameter. So then your script can detect what content to deliver based on $_GET['q'].
And here's another approach, this one from Wordpress.
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
This one specifically looks for requests to index.php and filters them out before doing the redirection.

Categories