Vanity URL and redirection - php

I am trying to make a redirection service, which can redirect users to their profile page, based on a vanity url.
The vanity URL will be of the format - www.site.com/username
This will redirect them to a page profile_redirect.php, which will echo the username (The functionality is deeper, but this is all I need help with).
I tried several of the answers posted here, but I am unable to get this to work. At the moment, it does nothing and just gives me a page not found error. I am running this on wamp, and I have localhost's alias set to "proj", so I call "http://proj/username", but this gives me page not found.
My current htaccess file (Relevant portion)
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)$ $1.php [NC]
RewriteCond %{REQUEST_FILENAME} >""
RewriteRule ^([^\.]+)$ profile_redirect.php?user=$1 [L]
My current profile_redirect.php file
$getName=$_GET["user"];
echo $getName;

Have your rules like this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)/?$ $1.php [L]
RewriteRule ^([^\.]+)/?$ profile_redirect.php?user=$1 [L,QSA]

Related

.Htaccess Rewrites File Prevents new Web Pages from being Viewed, 404 Error

I am editing a website for a client and have hit a bit of a roadblock. I can edit and publish existing pages just fine, but if I were to create a new .php or .html page and try to view it online, I get sent to the 404 error page. I can take the new page I want and rename it to overwrite an existing page and everything loads fine.
I have tried some different ways to do the rules I need, but they either try to find the page in the root (When it's actually in /application/views/); or it works and I have the same problem as before.
I need help making a new .htaccess file that allows you to go to website.com/signup and it takes you to /application/views/signup.php. Below are some examples of what I've tried so far.
Original .htaccess:
RewriteEngine on
RewriteCond $1
!^(index\.php|images|css|js|uploads|db|fonts|blogger|support|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Another version that has the same issue.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*$ index.php/$1 [L]
Lastly, a version that allows new pages to be viewed but looks for them in the root folder. Eg. The requested URL /signup.php was not found on this server. I imagine if this is modified it will work, but I have no experience with .htaccess rules. Much appreciated to anyone that can help me and future browsers!
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([^\.]+)$ $1.html [NC,L]
RewriteRule ^([^\.]+)$ $1.htm [NC,L]
You should be able to add a couple of conditions (RewriteCond directives) to the original .htaccess file rule:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|uploads|db|fonts|blogger|support|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
So, only if the request does not map to an actual file or directory will it be rewritten (to the front controller).

How to remove .php extensions without changing all links

How I can do to remove .php extension (and html if it's possible in all link) without changing all links ?
I have search and I have found this solution :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([^\.]+)$ $1.html [NC,L]
It's work good but there are a problem, in fact in my header (or all post form), my link looks like this :
Action
So then users click on, the url is :
mysite.com/action.php
How I can do to all my url don't have .php exentison without changing all links in my website
Thank you
This will Work
RewriteEngine on
RewriteRule ^(.+)\.php$ /$1 [R,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [NC,END]
On apache 2.4 and later, END flag is used to prevent infinite loop error.

.htaccess - Make /some-address the same as /page.php?id=1

I am currently working on a project where I have to redirect a user after a form has been submitted.
Once the form is submitted I redirect the user to a url based on what they entered into the form. An example of what the URL might look like:
/oslo-norway-copenhagen-denmark
I would like /oslo-norway-copenhagen-denmark to display in the users URL bar, would would like it to represent the page /page.php?id=oslo-norway-copenhagen-denmark.
I've currently got this working:
Options +FollowSymLinks
RewriteEngine On
DirectoryIndex search.php
RewriteBase /services
RewriteRule ^formmail/([^/]*)$ formmail.php?id=$1 [L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
The only problem is that this makes /formmail/oslo-norway-copenhagen-denmark work, but how do I go about making this only /oslo-norway-copenhagen-denmark? I tried removing the /formmail/ but this didn't work
Could anyone help me solve this problem? Thank you very much!
All you need to use is this in your .htaccess file:
RewriteEngine On
RewriteRule ^([^/]*)$ /services/page.php?id=$1 [L]
Make sure you clear your cache before testing it. It will leave you with your desired URL.

why .htaccess is showing me "Internal Server Error"?

In my site I removed .php extension from the url using .htaccess rules. So that I can go to any page without .php extension. e.g :
http://localhost/aponit/dev/zones (there are no .php extension)
Now,I have a link in a php page to update a form data. Link is bellow :
<a class="btn btn-success btn-xs" href="<?php echo SITE_URL."update/$zone_id"?>" >Edit</a>
This link showing following url :
http://localhost/aponit/dev/update/54
But unfortunately It's showing me error message :
Internal Server Error
I am using following .htaccess rules :
ErrorDocument 404 /not-found.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
RewriteRule ^update/([0-9]+) update?z=$1 [L]
Trying to:
ErrorDocument 404 /not-found.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
And post detail log file???
The .htaccess code that yuo have provided will not work correctly if the site does not run under root domain. For example
RewriteRule ^update/([0-9]+) update?z=$1 [L]
This code block tells the server that any URI starting with "update" following a number will be rewritten. but in your case, the URI always starts with "aponit/dev/". So this code will not work any time.
Solution
Set up a virtual host. You will get a lot of available tutorials online how to setup a virtual host based on your server software(XAMPP/WAMP etc)
If you just want to remove .php from url, use the following code in .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

I recently did an htaccess redirection of php site, The redirect itself works, but now I cannot access the admin panel. All it shows is a blank page

I recently did an htaccess redirection of php site,
The redirect itself works, but now I cannot access the admin panel. All it shows is a blank page.
Here is the htaccesscode:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Make sure .php file exists before adding it for each request:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)$ $1.php [L]

Categories