I'm trying to rewrite a url such as:
www.domain.com/file/name.php?id=1
to without the .php like this:
www.domain.com/file/name?id=1
I have tried altering the htaccess that's placed in the file directory to:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC]
But it just doesn't work and every time I've tried typing in www.domain.com/file/name?id=1 I get a 404 error.
I've tried all the answers on here relating to this to make sure its not a duplicated question and also double checked to make sure mod rewrite is enabled but I'm at a complete loss now!
Any help would be much appreciated.
Try this: RewriteRule ^name$ name.php
This is from my own .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^name$ name.php
</IfModule>
Related
I am writing a program and run it locally with xampp. the full path to the program is http://192.168.101.103:7777/WeltesMaft/index.php
I am writing a .htaccess in C:\xampp\htdocs\WeltesMaft\
containing this
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/([^/]+)$
RewriteRule .* http://192.168.101.103:7777/WeltesMaft/%1 [L]
but somehow my htaccess doesnt work. it still showing like this
http://192.168.101.103:7777/WeltesMart/_resources/main.php
which is in index.php i am including main.php. So when user enters index.php, it will redirect to main.php in an instant.
I want to hide it so that the url looks like this,
http://192.168.101.103:7777/WeltesMart/
Please help me what am i doing wrong here...
You can use this code also.....
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ _resources/main.php/$1 [L,QSA]
</IfModule>
Change RewriteBase to your directory name and change the htaccess rule as following and try again:
RewriteEngine On
RewriteBase /WeltesMaft
RewriteCond %{REQUEST_URI} ^/([^/]+)$
RewriteRule / index.php [L]
My apologizes if this has been answered but I haven't found anything that works for me. This is on a cheap godaddy shared server, php based.
I have tried a few RewriteRules but to no avail.
Basically, if you are linked to something like :
www.example.com/items/apple
It would keep that in the url, but instead go to:
www.example.com/items
In the items page, I would be able to get the /apple part and do with it as I will. Either having it passed in through parameter like ?dir=apple or just as the /apple. My problem right now is the redirecting. My .htaccess file looks like this
RewriteEngine On
Options -Multiviews
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^item/([^/]+) /item?dir=$1 [NC]
Try adding an additional condition and/or switch the order:
RewriteEngine On
Options -Multiviews
RewriteRule ^item/([^/]+) /item?dir=$1 [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}.php -f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
I have my url as
http://public_html.com/sub-products.php?catid=42
I want it to look like
http://public_html.com/sub-products/catid/42
I have made a .htaccess file too. but still there is no change in my url
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^sub-products/catid/([0-9]+)/?$ sub-products.php?catid=$1 [L]
Do i need to change anything in my php file also?
what code is required for php file to use .htaccess file?
Your rule in .htaccess rewrite only incoming seo-pretty URls to URL with php script filename. But if you need construct url as /sub-products/catid/42 you have to change you PHP code, that render HTML output.
for example
while ( $products as $product ) {
echo '<a href="/sub-products/catid/"'.$product['id'].'>'.$product['name'].'</a>;
}
Maybe there is a purpose to your line
RewriteRule ^(.*)$ $1.php
But i can't see it for the need you have, so i guess a
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^sub-products/catid/([0-9]+)/?$ /sub-products.php?catid=$1 [L]
should work.
Edit :
This should work if you are trying to access the http://public_html.com/sub-products/catid/42 url
(or http://public_html.com/sub-products/catid/42/ btw)
If what you want is going from
http://public_html.com/sub-products.php?catid=42
to
http://public_html.com/sub-products/catid/42/
You might need something like this :
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{QUERY_STRING} ^catid=([0-9]+)$
RewriteRule ^sub-products.php$ /sub-products/catid/%1/? [R=301,L]
And then, you can use what i said before.
I am not sure if you switched your first statment around, but it looks in your .htaccess file you want to rewrite the other way around. Maybe this would work.
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} catid=([0-9]+)$
RewriteRule ^/?sub-products.php /sub-products/catid/%1 [R,L]
If you want to have a more generic rule, and not just for catid this RewriteRule could work:
RewriteCond %{QUERY_STRING} ([a-zA-Z]+)=([0-9]+)$
RewriteRule ^/?sub-products.php /sub-products/%1/%2 [R,L]
Hope it helps you forward.
EDIT: Made some changes after testing it on my system.
Ok, I have a link http://www.cuadmemo.com/posts.php?id=2861 and want it to show like this
http://www.cuadmemo.com/2861
I have in the .htaccess file this.
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ posts.php?id=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ posts.php?id=$1
Have tried a number of different solutions that I read on this site but none seem to work this far. Any suggestions to fix this would be greatly appreciated.
RewriteEngine On
RewriteBase /
# Make sure we don't rewrite directories and existing files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)/?$ posts.php?id=$1 [L]
I have learned about URL Rewriting using .htaccess, very recently. As a beginner it has become difficult task for me :(
I can rewrite a specific url, like:
RewriteEngine On
RewriteRule ^nothing/?$ abc.php [NC,L]
But I'm not satisfied with it and need your help.
My .php files are kept in a folder named files. So I have URLs like:
www.mysite.com/files/login.php?lf=student
www.mysite.com/files/login.php?lf=admin
www.mysite.com/files/s_home.php
www.mysite.com/files/recharge.php
What I want is simple:
mysite.com/login
mysite.com/login
mysite.com/s_home
mysite.com/recharge
Can anybody help?
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Even though this will do as you've asked, why are you trying to hide the .php extension?
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{DOCUMENT_ROOT}/files/$1.php -f
RewriteRule ^([^/]+)/(.+)/?$ /files/$1.php?lf=$2 [L,QSA]
RewriteCond %{DOCUMENT_ROOT}/files/$1.php -f
RewriteRule ^([^/]+)/?$ /files/$1.php [L]