.htaccess Save not existing url - php

Is there a way to save the not existing url?
Lets say i go to:
www.domain.com/channel/abcd1234/
The folder abcd1234 doesn't exitst so you will be redirected to:
www.domain.com/channel/error/
Is there a way to save the first url? and getting it on the error page in a php variable?
<ifmodule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) http://domain/channel/error/ [R=301,L]
</ifmodule>

Instead of your rewrite rule you should use:
ErrorDocument 404 /channel/error/
Original 404 URL will be available to you as $_SERVER['REQUEST_URI']

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^/]+)$ $1.php [L]
replace by it.i wish it help you

Related

htaccess url folder redirect

I am trying to change my url to try to stop it from showing the language folder in the url for example:
change from
www.site.com/en
change to
www.site.com
so I am looking to remove the
en
from the url but still accessing that directory. This is what I have so far but it does not seem to do the trick:
RewriteEngine on
RewriteBase /mysite/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .+ en=$0 [L,QSA]
Thank you.
This should do it:
RewriteEngine on
RewriteBase /mysite/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ en/$1 [L,QSA]

How to rewrite an url via htaccess

I am trying to rewrite the following URL via .htaccess:
http://website.com/dealer_home.php?id=dealer1
The result I am aiming for is this, where dealer1 is the username which is set as variable:
http://website.com/dealer1
I have tried this rule in .htaccess:
RewriteEngine On
RewriteRule ^([^/]*)$ /dealer_home.php?id=$1 [L]
However I get "Internal Server Error" message when trying to load any of the website pages.
Can you provide some advice where I am doing wrong?
EDIT:
I tried also RewriteRule ^(.*)$ dealer_home.php?id=$1 [PT] but no success.
Thank you!
Maybe it's a conflict with existing files/folders and root uri.
Try this code instead
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ /dealer_home.php?id=$1 [L]
This rule will match every url like domain.com/something if something is not an existing file or folder.
So if you have other rules then you should put them above this one.
EDIT: to avoid duplicate content and to redirect old format to new url format
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/dealer_home\.php\?id=([^&\s]+)\s [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ /dealer_home.php?id=$1 [L]

htaccess removing extension in two different pages

i used the htacess to remove the php extension and it worked but i also have a page called profile wich i shorten but that make the pages names being taken as a a profile user name, is there a way to fix this?
the results i want is like this:
localhost/path_folder/index.php to localhost/path_folder/index
and
localhost/path_folder/profile?username=name to localhost/path_folder/name
and my htaccess code for the removing extension is:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC]
and the code for the profile page is:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /path_folder/profile.php?username=$1
Put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
## First try To internally redirect /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ /path_folder/$1.php [L]
## if PHP file not found then load profile page
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/?$ /path_folder/profile.php?username=$1 [L,QSA]
If you want to remove file extension
RewriteEngine On
RewriteRule ^path_folder/([^/]*)$ path_folder/$1.php [L]
Shorten URL for userprofiles
RewriteEngine On
RewriteRule ^path_folder/profile/([^/]*)$ path_folder/profile?username=$1 [L]

htaccess redirect hiding the php extension or pass a parameter

I'm using a main index.php file to handle the most of requests, so my basic .htacess looks like this one:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /index.php?page=$1 [L]
But i've got some requests (like /signin or /signout) that needs to be handled directly from .php file with the same name (/signin.php and /signout.php).
So my .htaccess rules needs to be: "if %{REQUEST_FILENAME}.php does not exists redirect to /index.php?page=$1 else redirect to %{REQUEST_FILENAME}.php".
How can i do this?
Replace your code with this:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php !-f
RewriteRule ^(.+)$ /index.php?page=$1 [L,QSA]
This will send everything to /index.php that is not a valid file or directory and doesn't have a matching .php existing in your filesystem.
you could handle this in your index.php, just redirect every call on index.php. There you can readin all parameters given in URL.
$aParams = explode("/", $_SERVER["REQUEST_URI"]);
Now you can handle all parameters and redirect if you need to.

htaccess remove .php and keep query string

Here is my .htaccess file right now.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [NC,L,QSA]
This works in the fact that it makes my pages accessible when not using the .php extension.
Old = domain.com/test.php
New = domain.com/test
The bad thing is that when I send get data with the following link the data is not passed. I thought the QSA option did that, whats the deal?
domain.com/test?id=1
Matching the entire query string and appending it to your new URL using a back-reference should work.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)$ $1.php?%1 [NC,L,QSA]
Here's a more simple solution. We use it on our team project. It will work not only in the root, but in any directory of your website.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
If you are passing id then you have to use this code
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^test/(.*)$ /test.php?id=$1
now you can access
domain.com/test?id=1
from
domain.com/test/1

Categories