Dynamic url in php without filename - php

I couldn't find exact answer to my question so I am posting this. Maybe it involves htaccess configuring.
What I would like is to have http://www.example.com/static_folder/dynamic_parameter
the above url should run the index.php in static_folder and I should be able to get that dynamic_parameter and output accordingly.
How to achieve this?
Thanks.

A littlebit of htaccess can do it pretty easily. As for example, save this rule in the root directory's htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^/static_folder/index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /static_folder/index.php [L]
</IfModule>
Then use $_SERVER['REQUEST_URI'] inside /static_folder/index.php to get the dynamic_parameter.
Is it what you are trying to achieve?

Try this in your .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
then you can do:
sudo a2enmod rewrite
sudo service apache2 restart
Also check answers for this question: How to remove all .php extension from a dir and all of its subdirectories?

I believe the OP asked for index.php inside static_folder to get the variable, so, something like this, should do the trick:
RewriteRule ^static_folder/([^/]+)$ static_folder/index.php?variable=1$ [NC,L]

Related

how to remove index.php?file= using .htaccess to get last url

Here, I want to remove index.php?file= for every urls of my website. I have tried many tutorial of .htaccess,but unlucky to create correct one.
Actual url as below:
http://localhost/test/json/iscore2/index.php?file=home
http://localhost/test/json/iscore2/index.php?file=contact
Then I want to make like this :
http://localhost/test/json/iscore2/home
http://localhost/test/json/iscore2/contact
Probably you already have setted up rewrite module in apache2
(sudo a2enmod rewrite and host settings for AllowOverride)
I'd start with something like this in iscore2 folder
<IfModule mod_rewrite.c>
RewriteEngine on
# Play with this if redirection fails
#RewriteBase /
# if it is not a file or folder, rewrite to index.php?file=<value>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?file=$1 [L,QSA]
</IfModule>

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'].

Remove php file extension from the url

I know this question has been asked like a 1000 times and I have probably tried like a 1000 suggestions as well, still no go.
I would like to remove the php file extension and substitute it with a slash (the slash is not so important but to remove the extension is).
My project is located here:
localhost/~fn/MyProject/
It contains two folders, public and includes. So all the public files are in the public folder: localhost/~fn/MyProject/public/index.php
I have tried so many suggestions already but most of them simply don't work. I am getting either a Internal Server Error, Forbidden or 404. I am putting the .htaccess to the public folder. Mod rewrite is on. No success with anything on stackoverflow and neither external resources ( e.g. http://alexcican.com/post/how-to-remove-php-html-htm-extensions-with-htaccess/ ). For example using the rewrite rules from the metioned webpage shows me 403 Forbidden to even access the index.
Any hints of what I may be doing wrong? I am really stuck. Thanks a lot!
If your htaccess is in public project folder, try with this code
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteCond %{THE_REQUEST} ^.*public/(.+)\.php
RewriteRule ^(.*)$ /~fn/MyProject/public/%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ /~fn/MyProject/public/$1.php [L]
If the directory has an Index.php in it, the url will not show the file name when you browse to that folder. example.com/index.php would just show as example.com. You could use PHP includes to pull in each page to the index page in order to hide the file names but this isn't the best practice in general.
Put this in a .htaccess file in localhost/~fn/MyProject/ (so the file will be localhost/~fn/MyProject/.htaccess):
RewriteRule ^(.+?)\.php$ $1/ [QSA,L]
This captures anything ending in .php, strips off the .php, adds a /, and appends any query string as needed.
If you want only remove extension ".php" from index.php you should use mode_rewrite
RewriteRule ^index.php$ index // or
RewriteRule ^index.php$ /
But best way is implements mechanism to rewrite all urls and manage in your front script (index.php);
If you want use mode_Rewrite you shuold check if your mod_Rewrite is enabled on your Apache server. Go to apache.conf and check if line
LoadModule rewrite_module modules/mod_rewrite.so
is uncomented, dont forgot restart apache!
than you can On rewrite rule type in your apache.conf
<IfModule mod_rewrite>
RewriteEngine On
</IfModule>
or include something like in .htacces
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php?%{QUERY_STRING} [L]
Now all signs from urls will going to your once index.php. Here you can do with this everything what do you need to do for example u can call some controller.
If you get 403 Forbiden dont forgot check chmod of the file.
To remove the .php extension from a PHP file for example yoursite.com/demo.php to yoursite.com/demo, you can add the following code to the .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

Convert PHP Variable URL with mod_rewrite to clean URL

I need to convert a link that contains php variables into a clean link that can be picked up and indexed by Google. I've looked at ways to do this, and using mod_rewrite apparently is the best way to handle this.
Currently my URLs look like this:
http://mysite.com/jobs/view/?j=senior-model-validator-groep-risk-management
I'd like to end up looking like this
http://mysite.com/jobs/view/senior-model-validator-groep-risk-management
It's probably really easy but sadly I know nothing about .htaccess. I tried a few things but I end up with nothing remotely close :-)
Thanks in advance!
Edit: Here's my full .htaccess file based on the help so far:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^jobs/view/(.*)$ jobs/view/?j=$1 [L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
You may use an .htaccess file at the root of you web site with these rules:
RewriteEngine on
RewriteRule ^jobs/view/(.*)$ jobs/view/?j=$1 [L]
Do not forget to filter/validate GET input in the script dealing with the data...

Mod-rewrite.htaccess not working

Hi I previously posted a question on to Stack Over flow and was lucky enough to receive some assistance to my initial question which helped me better understand that what i was trying to achieve was possible.
The previous question is here: Visit php url shortening
I am building a system where users can share their profile on other websites so i am wondering is it possible to shorten the actual url which would provide a link to their profile which would be something like this, www.somedomain.com/users/profile.php?user=myusername to simply cut out the users folder and the profile page and so something like this: www.somedomain.com/myusername
the code I have is:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule /(.*) /users/profile.php?user=$1
Sadly two things happen, firstly the code doesn't work and secondly when i add this code the css on the original long domain stops working.
Any suggestions would be appreciated
code doesn't work: of course it won't because it's incorrect. Really wondering how did you end up accepting other question without properly testing it. Anyway take this code and replace your existing .htaccess code:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php/?$ - [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ users/profile.php?user=$1 [L,QSA,NC]
This is serverfault question.
Are your running apache?
Are apache mod_rewrite module enabled?
debian: a2enmod rewrite; /etc/init.d/apache2 restart
Are enabled override for this directory? In case using default vhost:
<Directory /var/www/>
#AllowOverride None
AllowOverride FileInfo
</Directory>
What are showing error.log?
tail /var/log/apache2/error.log
Try simple version:
Code:
RewriteEngine On
RewriteBase /
RewriteRule ^profile.php profile.php [QSA,L]
RewriteRule ^(.*)$ profile.php?user=$1 [QSA,L]

Categories