Make a friendly URL with 2 parameters on PHP with Apache - php

So I am in a bit of a soup here.
I need to make a friendly URL.
This is the URL where I enter the search values project_name and version
localhost:8080/demo_webpages_project/retrieval.html
This is where it takes me to on submitting the values
localhost:8080/demo_webpages_project/download.php?project_name=QT&version=3.1.2
I want the URL in the following form
localhost:8080/demo_webpages_project/download/QT/3.1.2
I tried doing this using .htaccess but not really finding a solution
The following is the code for the .htaccess file.
<IfModule mod_rewrite.c>
#Turn the engine on
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#attempt1
#RewriteRule ^(.*)/$ download.php?project_name=$1&version=$1
#RewriteRule ^/(.*)$ download.php?version=$1
#attempt2
#RewriteRule ^(.*)/(.*)$ download.php?project_name=$1&version=$2 [L,NC,QSA]
#RewriteRule ^(.*)$ download.php?version=$1
</IfModule>
I would be really grateful if someone could help me out with this. This is the first time I am working on something on this and am lost.
Thanks

Have this .htaccess inside /demo_webpages_project/:
Options -MultiViews
RewriteEngine on
RewriteBase /demo_webpages_project/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ download.php?project_name=$1&version=$2 [L,QSA]

According to this article, you want a mod_rewrite (placed in an .htaccess file) rule that looks something like this:
RewriteEngine on
RewriteRule ^/news/([0-9]+)\.html /news.php?news_id=$1
And this maps requests from
/news.php?news_id=63
to
/news/63.html
Another possibility is doing it with forcetype, which forces anything down a particular path to use php to eval the content. So, in your .htaccess file, put the following:
<Files news>
ForceType application/x-httpd-php
</Files>
And then the index.php can take action based on the $_SERVER['PATH_INFO'] variable:
<?php
echo $_SERVER['PATH_INFO'];
// outputs '/63.html'
?>

Related

How to add url parsing in Htaccess

I looking to parse only one specific url in htaccess in following format
https://example.com/apps/store/customdesign/shopid/2/designid/Design_10
into
https://example.com/apps/store/customdesign.php?shopid=2&designid=Design_10
I tried following things
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/customdesign$
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)$ /customdesign.php?$1=$2&$3=$4
</IfModule>
I assume you are looking for a Mod_Rewrite solution, so first make sure you have the mod rewrite extension active, then write your URL Rewrites;
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
^apps/store/customdesign/shopid/([0-9]+)/designid/([0-9A-Z_]+)$ /apps/store/customdesign.php?shopid=$1&designid=$2 [QSA,NC,L]
</IfModule>
I have provided the solution I would most-likely use if this were my problem, however you should look into RegEx as well as your specific installation and hosting permissions before continuing with URL Rewrites. Spend some time and do some research!

How to create virtual folders using php?

I am using php with aws elasticbeanstalk.
I want to create folder that accept this url :
www.example.com/481818
where 481818 present an id that is changed.
so i want to create file that accept every request form this structure www.example.com/integer. and fetch the id.
i don't know how to create this file.
I can do it with this url : www.example.com/?id=4545454 . but I don't want this url structure.
thank you
You have to rewrite all non-existent URLs to a script. On Apache you can make it adding these rules to .htaccess file in your www root.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+)$ /index.php?id=$1 [L]
</IfModule>
Put this in you .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^([0-9]+)$ index.php?id=$1 [NC,L]
</IfModule>
It should do what you want, I didn't use elastic-beanstalk before, hopes I can help you.

How can I mask a "get" URL and make it look look like a file?

I would like to first apologize for my choice of words. I haven't been in the web dev business to properly word the title, so there's that.
I'm using Apache Server and I have a .htaccess file in the root folder of my project. It specifies a 404 page and also a rewrite condition for removing the .php out of a file in the URL:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ $1\.php
</IfModule>
I have a users section on my page "social.php" and retrieve them using a GET variable in my code.
Example URL:
www.mysite.com/users/social.php?username=someuser
However, I wanted to know how I could make it look something like:
www.mysite.com/users/someuser
Thanks in advance!
Have your root .htaccess as this:
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteRule ^users/(\w+)/?$ /users/social.php?username=$1 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ $1.php [L]
</IfModule>
This is only partly a mod_rewrite problem at this point.
The first part is to route URLs like /users/[a-z]+ to some script /social.php
RewriteRule ^/users/(.*)$ social.php?username=$1
The second part is inside social.php you need to get the username "someuser" in $_GET['username'].

how to change url using htaccess

I want to show the URL
http://www.example.com/blog/feedback
as
http://www.example.com/feedback
How can I do so using .htaccess?
You will need mod_rewrite to do this.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^/feedback$ /blog/feedback
</IfModule>
Have a look here for a tutorial information on mod_rewrite.
EDIT:
If you are trying to move JUST /blog/feedback to /feedback then you will have to first move your base from /app/webroot/blog/ to /app/webroot/, edit all your rules to account for it and then add your new rule. Could be done like so:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /app/webroot/
RewriteRule ^/blog/index\.php$ - [L]
RewriteRule ^/blog/feedback$ /feedback [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule /blog /app/webroot/blog/index.php [L]
</IfModule>
NB: This should work, but I'm not at my work station right now so I can't check it :( If anything goes wrong it's probably a typo somewhere - let me know.

i need some help creating Htaccess

i need to hide the extensions of my webpage and also want to let the user put the links in both (lower and upper) case:
Example:
the file name is demo.php
www.example.com/demo
www.example.com/DEMO
www.example.com/Demo
Running PHP in a LAMP server, no access to php.ini, just .htaccess
Actualy im using a file like this:
RewriteEngine On
RewriteBase /
RewriteRule ^/(OUTSOURCING|outsourcing|Outsourcing)$ outsourcing.php [NC,L]
And i m reciving this error:
Not Found
The requested URL /outsourcing was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
RewriteEngine On
RewriteBase /
www.example.com/DEMO www.example.com/Demo or doing www.example.com/DEMO/page2
RewriteRule ^/(DEMO|demo|Demo)/(.*)$ demo.php?=$1 [NC,L]
RewriteRule ^/(DEMO|demo|Demo)$ demo.php [NC,L]
RewriteRule ^/(D|d)emo$ demo.php [NC,L]
or pass anything www.example.com/DeMo www.example.com/bob to demo.php
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ demo.php [NC,L]
you may want to test if your allowed .htaccess RewriteRule /*$ http://google.com [R][L]
here is a good way to do it with case insensitive method
EDIT:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ${lc:$1}.php [NC]
this way anything entered will be redirected to a php file.
edit : this way your js and css file can still run
RewriteRule ^/[Dd][Ee][Mm][Oo]/?(.*)$ demo.php [NC,L]
will redirect any capitalization of "Demo" to demo.php
First add this line in the <VirtualHost> section OR at the end of your httpd.conf file (to enable lc function in .htaccess for later use):
RewriteMap lc int:tolower
Then have these rules in .htaccess file:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteCond %{THE_REQUEST} ^GET\s.+\.php [NC]
RewriteRule ^(.+)\.php$ /$1 [NE,R=301,L,NC]
RewriteCond %{REQUEST_URI} !\.php$ [NC]
RewriteCond ${lc:%{REQUEST_FILENAME}}.php -f
RewriteRule . ${lc:%{REQUEST_URI}}.php [L]
First rule in .htaccess is doing external redirect by making a URI of /index.php to /index
Second rule in .htaccess is doing internal redirect by makign a URI of /INDEX to /index.php by lowercasing the URI. Assuming you have filename index.php physically present.
That way you can always write URLs without .php extension.

Categories