.htaccess - Redirect - php

What I want is to redirect by .htaccess
http://example.com/omgitsaname
I want really to redirect, or better, to stay the same but to get the information from
http://example.com/product?id=omgitsaname

If you want to use these types of URL's in your browser
http://example.com/omgitsaname
You can do it this way and make sure the that the URI is not a real directory or a file.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /product?id=$1 [NC,L]

Here is how you can use Rewrite Rules via .htaccess, in Apache
RewriteEngine On
RewriteRule ^product/?$ do/something/here [NC,L]
RewriteRule ^([a-z]+)/?$ product?id=$1 [NC,L]
Make sure the mod_rewrite module (or equivalent) is enabled.
Also add the following rule: AllowOverride all (or equivalent) to allow .htaccess to work.
Note: Modify the regular expression as per your needs.
Edit: Thanks to #4sha to fix the redirect loop.

Related

GET Variable lost on RewriteRule

I have written a redirect to change quiz.php?quiz=1 to /quiz/1 however PHP can no longer pickup the GET Variables, is there anything i'm missing? This is my htaccess file:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^quiz/([^/]*)$ /quiz.php?quiz=/$1 [QSA,L]
You have error in RewriteRule ^quiz/([^/]*)$ /quiz.php?quiz=/$1 [QSA,L]
There should not be slash at redirect target, so correct is RewriteRule ^quiz/([^/]*)$ /quiz.php?quiz=$1 [QSA,L]
I tried this on localhost and it is working properly. Try debugging via e.g. var_dump($_GET);
In this specific case, you probably need to disable content negociation and AcceptPathInfo features by adding the following lines to your .htaccess file:
AcceptPathInfo off
Options -MultiViews
Content negociation can internally "rewrite" "quiz" into "quiz.php". And AcceptPathInfo "quiz.php/a/b/c" to "quiz.php" with $_SERVER['PATH_INFO'] = '/a/bc/c'. They both happen before rewriting, bypassing your rule.

.htaccess url rewriting not working?

I'll be honest in saying I have very little experience with .htaccess as I've always wanted to stay away from it as best I can. However, I've recently wanted to tidy up my urls and I've found that it's possible through .htaccess and rewriting.
Basically, I want to rewrite a url like:
www.mysite.com/profile.php?id=48194
To something like:
www.mysite.com/profile/48194
Here's the code I have currently:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^profile/(.*)/$ profile.php?id=$1
The line I'm trying to use is on the very bottom, RewriteRule ^profile/(.*)/$ profile.php?id=$1. The rest is used to remove the page extensions from the urls. I've changed $1 to $2 thinking perhaps it was conflicting with the code above, but nothing changed.
I also removed all the code except for RewriteEngine on and the last line thinking maybe the codes were conflicting but, again, nothing changed or worked. The rest of the code does work, removing the extensions from urls that is, so I know the rewrite thing is on.
Could someone try to break down and explain what I did wrong and how all this works? As well as providing a working example of the thing I'm trying to accomplish?
Thanks in advance!
Change order of your rules and use MultiViews option. Option MultiViews is used by Apache's content negotiation module that runs before mod_rewrite and and makes Apache server match extensions of files. So /file can be in URL but it will serve /file.php.
RewriteEngine on
RewriteBase /
RewriteRule ^profile/([^/]+)/?$ profile.php?id=$1 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]

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]

.htaccess being ignored? Not redirecting like it's supposed to

Okay I'm trying to use Lando (landocms.com) and I'm trying to get the pretty urls option to work.
Basically by default Lando creates link like: domain.com/index.php/page. Supposedly, there is a way to remove the index.php so the links become: domain.com/page. I have created an .htaccess as directed, however it does not work.
Here is the .htaccess I am using:
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
I have tried alot of variations, /index.php/, index.php? and plenty more but none work. According to HostGator everything should be fine. Any thoughts? I think I'm going crazy haha.
Thanks!
Rewriting for a CMS is a two-tier approach. First, you need to set your .htaccess (I have put a safer one here for you):
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .+ index.php [QSA,L]
Then, LandoCMS allows you to remove the index.php from the generated addresses, by means of turning on the appropriate setting in the administration panel. See this link for more information.
If the .htaccess content I've given you doesn't work, then simply use the one that the CMS has given you.
You want to remove the index.php part from any URL, but process the incoming, friendly URLs through index.php nevertheless
RewriteEngine On
# remove index.php and redirect client
RewriteCond %{ENV:REDIRECT_SEO} ^$
RewriteRule ^/?index.php/(.*) /$1 [R,L]
# process friendly URL
RewriteCond %{REQUEST_URI} !^/index.php/
RewriteRule .+ /index.php/$0 [E=SEO:1,L]
The environment setting E=SEO:1 prevents an endless loop.

How to make a PHP GET Request method like this "http://somesite.com/games/213123"?

Hi guys I'm wondering how to make this kind of requests to the server I looked at many sites and they use this technique. For example gametrailers.com => http://www.gametrailers.com/video/level-six-diablo-iii/**721239**. I know that a GET request is made by using parameters like this: http://somesite.com/page.php?param=1. So how to make it like gametrailers. I hope you understand my question and I doubt that "721239" is a folder on the server with an index page inside of it.
You need to create a file placed in the folder near your script with name .htaccess
In this file you need to define rewriting rules. The contents of the file are:
RewriteEngine on
RewriteRule ^games/(.*)$ games.php?id=$1 [L]
In this case
http://somesite.com/games/213123
will be transformed into http://somesite.com/games.php?id=213123
The more convinient way is to do url rewriting. (wiki)
For example, you can have a .htaccess like this, well explained in this guide from symfony:
<IfModule mod_rewrite.c>
RewriteEngine On
# we skip all files with .something
RewriteCond %{REQUEST_URI} \..+$
RewriteCond %{REQUEST_URI} !\.html$
RewriteRule .* - [L]
# we check if the .html version is here (caching)
RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
# no, so we redirect to our front web controller
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
You can achieve this using MultiViews in Apache2 (http://httpd.apache.org/docs/2.0/content-negotiation.html).
MultiViews (when enabled) instructs Apache to build a parameter map when a resource doesn't exist, so for www.foo.com/app/game/14, if www.foo.com/app.php exists and app/game and app/game 14 don't, it can be set up to translate that to www.foo.com/app.php?type=game&article=14
Some people also use mod_rewrite, but I'm not sure that's the preferred approach.

Categories