.htaccess redirect directly after domainname - php

I want that if a user types in www.example.com/article-title, it gets the data from www.example.com/index.php?title=article-title. Right now it only works if a user types in www.example.com/article/article-title. I want to remove that article/.
This is what I have right now in my .htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC]
RewriteRule ^article/([0-9a-zA-Z]+) index.php?title=$1 [NC,L]
If I remove article/ from the last line in my .htaccess file, it doesn't work at all.
The $1 should be article-title, but if I remove article/, $1 becomes index.
Does anyone know how I can change the .htaccess in order to let users type in www.example.com/article-title?

You just need
RewriteEngine on
RewriteRule ^(.*)$ index.php?title=$1 [NC]
tested here with
input: https://www.example.com/article-title
output: https://www.example.com/index.php?title=article-title
However i suppose this is actually what you really want, because by the looks of things you are not locking for a redirect, but something like:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
And then in the index.php file you have to "manipulate" the url requested

Related

.htaccess multiple RewriteRule's doesn't work

The multiple RewriteRule's doesn't work into my .htaccess file.
To get direct into the point, i have this lines of code into my .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ ./index.php?lang=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/$ ./profile.php?page=$1 [L]
The problem is when i add a parameter into my domain, let's suppose www.eaxmple.com/something, i land always to home page. What i want to do is when i set a parameter with slash at the end to go to profile.php and without slash to move into index.php. Even if i tried to put a parameter i always move to index page.
Can someone help me?
.* will match everything including trailing /.
Try rules in this order:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L][
RewriteRule ^(.+)/$ profile.php?page=$1 [L,QSA]
RewriteRule ^(.*)$ index.php?lang=$1 [L,QSA]

Remove index.php and parameter name from URL

Hi I am making a site where I want nice and clean links. I am trying to make a link with a get parameter look alot cleaner. Here is a sample link of how the link is at the moment:
http://www.example.com/index.php?item=cd-player
Here is how I want the link to look:
http://www.example.com/cd-player
I have been able to get rid of index.php from the url to leave just the parameter but I need help in getting rid of the "?item" bit as well.
Here is what I have tried so far:
RewriteRule ^index\.php(.*)$ /$1 [R,L,QSA]
RewriteRule ^(.+?)/?$ /?item=$1 [L,QSA]
Also, this may be a php code rather than a code for the .htaccess file, but if a user enters http://www.example.com/cd-player, how will it know that "cd-player" is the value of a get variable called "item"?
EDIT: The problem I am having with the answers below is that I am using the following code to remove file extensions and the code in the answers below treats the files as a query:
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Try this instead :
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?item=$1 [L]
You need 2 rules actually:
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /index\.php\?item=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L,NE]
# add .php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /index.php?item=$1 [L,QSA]
You need to do it like this. This will allow you to have clean links
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/.]+)$ $1.php [NC,L]
#This means if its not a real file
RewriteCond %{REQUEST_FILENAME} !-f
#This means if its not a real directory
RewriteCond %{REQUEST_FILENAME} !-d
#Then take the capture and rewrite it
RewriteRule ^(.+)$ /index.php?item=$1 [L]

Remove file extensions from my Php files

How can I remove .php extension from php files
htacess file
RewriteEngine On
RewriteCond
%{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
any help is much appriciated, Thanks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ $1.php [NC,L]
If you go to example.com/test/ it will load example.com/test.php
For hat you should define an entry point.
Then you can rewrite your URLs to a parameter of a specified file in that case the index.php.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ index.php?url=$1 [NC,L]
Then you can filter that parameter in your script and show the correct content.
You have an URL something like:
www.example.de/test-test
Then everything is rewritten to your index.php. Other possibility if you rewrite to an existing file. You can do something like this:
RewriteRule ^(.*)/?$ $1.php [NC,L]
Then you fetch everything behind the slash and call an existing PHP file.
Remove .php extension with .htaccess

Redirect /forgot-password/12345 -> /forgot-password.php?reset_key=12345

I'm attempting to set up .htaccess to allow for clean password reset URLs as follows:
The user is sent a link like:
www.url.com/forgot-password/12345
This should be rewritten as:
www.url.com/forgot-password.php?reset_key=12345
(and only for this page which is forgot-password.php - I want to keep the query strings for the other pages as-is.)
So far I have:
RewriteCond %{REQUEST_URI} !forgot-password
RewriteCond %{QUERY_STRING} (.)
RewriteRule ^(.*)$ /forgot-password.php?reset_key=$1 [L,QSA]
But it's not working. When I dropped the second line, it seems to trigger properly, but it's not passing the ?reset_key $_GET parameter in properly.
FYI, above this in my .htaccess, I already have the following:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Thanks in advance.
Keep your .htaccess like this:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteRule ^forgot-password/(.+)$ /forgot-password.php?reset_key=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]

htaccess behavior when accessing root

I've been using this htaccess code to pass on vars to redirect.php which handles the includes for me:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?(.*)/(.*)/(.*)$ redirect.php?value1=$1&value2=$2&value3=$3 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?(.*)/(.*)$ redirect.php?value1=$1&value2=$2 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?(.*)$ redirect.php?value1=$1 [L,QSA]
but i noticed that it will not go to redirect if all arguments is empty, so if i go to http://domain.com/ it will open index.php, but if i go to http://domain.com/any-param/ redirect.php handles it correctlty. How can I make it always use redirect.php as default, even when no additional URL parameters is set?
Your rules appear to be fine to me. Just add this line in the end:
RewriteRule ^$ redirect.php [L]
This will redirect http://domain.com/ to http://domain.com/redirect.php.
use to do it like this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/([a-z]+)?/?([a-z]+)?/?([-a-z0-9]+)?/?$ [NC]
RewriteRule ^([a-z]+)/?([a-z]+)?/?([-a-z0-9]+)?/?$ /redirect.php?value1=$1&value2=$2&value3=$3 [NC,L]
Note: Even with only 1 param, this will still work as it just leaves the other 2 blank. So you don't have to copy and paste thew same code while removing just one condition.
I found that this is not very scalable. Now I just redirect everything to the index.php and let that file fetch and dissect the URI to handle my system layout. This allows for a very scalable system.

Categories