.htaccess redirect works, but not rewrite - php

here is my .htaccess file, it works because the first rewriterule correctly redirects .html -> .php, but I am hoping to remove the .php extension also. if anyone could help me correct my code here I'd appreciate it.
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.+)\.html$ http://vbwtest.comeze.com/$1.php [R,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php

Try this.This will rewrite all your requests.
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)\.html$ $1.php [nc]

Remove the R flag from your rewrite rule:
RewriteRule ^(.+)\.html$ http://vbwtest.comeze.com/$1.php [NC]
The R in [R,NC] tells it to redirect instead of just rewriting.

Related

Issue with .htaccess for friendly urls

This is my currently my .htaccess file
Options -MultiViews
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC]
RewriteRule ^(.*)/edit/(\d+)?$ $1/edit.php?tag=$2 [NC]
As you can see it's set to hide the .php extension, but this then seems to break the edit rule. If I comment out
RewriteRule ^(.*)$ $1.php [NC]
The edit rule works fine, but I need both and cannot seem to get it to work, anyone see what the problem is and how to sort it.
[Edit]
I have a link like this and when all rules are active this is what doesn't work.
http://www.domainname.com/researcher/lists/edit/
and I get an 500 Internal Server Error.
Try rules as in your site root .htaccess:
Options -MultiViews
RewriteEngine on
RewriteRule ^(.+)/edit(?:/(\d+))?/?$ $1/edit.php?tag=$2 [NC,L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

How to rewrite URL through htaccess?

How can I rewrite the below url?
http://www.planethomes.in/project-detail.php?city=mumbai&project=Puraniks-Tokyo-Bay-Phase-1
And I want to rewrite to
http://www.planethomes.in/property/mumbai/Puraniks-Tokyo-Bay-Phase-1
But it's not happening. My .htaccess file below
Options -Indexes
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^property/([_0-9a-z-]+)/([_0-9a-z-]+) project-detail.php?city={R:1}&project={R:2}
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
You can do that using %{QUERY_STRING}:
RewriteEngine On
RewriteCond %{QUERY_STRING} city=(.+)&project=(.+)$ [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}/property/%1/%2 [R=301,L,QSD]
Then to internally rewrite the URL back also add below:
RewriteRule ^/property/([^/]*)/([^/]*) /project-detail.php?city=$1&project=$2 [L]
Make sure you clear your cache before testing this. You'll notice I've used R=301 which is a permanent redirect, I advise using R=302 while testing as this is a temporary redirect.

How to remove .php and get var after slash on .htaccess

Good day. I need to remove .php at the end of a file and also get the last /var into a php $_GET var from .Htaccess.
I want this url rewrite:
http://example.com/file/123
into
http://example.com/file.php?id=123
But this must work if file is index.php like this:
http://example.com/123
into
http://example.com/index.php?id=123
Thanks for all your help :)
You can use these rules in site root .htaccess:
RewriteEngine On
# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([\w-]+)/?$ $1.php [L]
RewriteRule ^([\w-]+)/?$ index.php?id=$1 [L,QSA]
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([\w-]+)/([\w-]+)/?$ $1.php?id=$2 [L,QSA]
This should meet your needs
RewriteEngine On
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^([0-9]+) http://example.com/file.php?id=$1 [NC]
This is a great article on the subject http://corz.org/server/tricks/htaccess2.php
Use this in .htaccess for remove the extensions :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)$ $1.php [NC,L]
And if you need a new extension add as many lines you want
RewriteRule ^([^.]+)$ $1.html [NC,L]
For more info read : https://alexcican.com/post/how-to-remove-php-html-htm-extensions-with-htaccess/

Remove .PHP and redirect through htaccess

Is that possible to hide the .PHP extension and redirect the URL at same time.
example : http://example.com/test need to redirect to http://someothersite.com
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^(.*)/test http://someothersite.com
But its not working.
Any idea ?
Thanks
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteRule ^test http://someothersite.com [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
The code above is not tested, but it should give you an idea about it. Use L flag after each set of rules, as it stops processing of the rest of the rules.
You need just this:
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)$ $1.php [R=301,L]
For cross domain rewriting:
RewriteRule ^test http://someothersite.com [R=301,L]
Allways use L = 301 for permanent redirectings.

htaccess RewriteEngine

I have an url which is http://www.urlbookmarking.com/bookmarks-details.php?bid=55
and I want it to be like
http://www.urlbookmarking.com/bookmark/55
I wrote in my htaccess:
RewriteEngine on
RewriteRule /bid/(.*) bookmarks-details.php?bid=$1
But when I go to the first URL the rewrite engine does not apply my rule. Is there any mistake, or conflict somewhere?
My full htaccess file written as follows
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteEngine on
RewriteCond %{HTTP_HOST} ^urlbookmarking.com [NC]
RewriteRule ^(.*)$ http://www.urlbookmarking.com/$1 [L,R=301]
RewriteEngine on
RewriteRule /bid/(.*) bookmarks-details.php?bid=$1
Please help me.
The line Options +FollowSymLinks is optional if already configured in httpd.conf
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^urlbookmarking\.com [NC]
RewriteRule ^(.*)$ http://www.urlbookmarking.com/$1 [R=301, L]
RewriteRule ^bookmark/([0-9]+)$ bookmarks-details.php?bid=$1 [NC, L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
A few things:
RewriteEngine On only needs to called once, though this may not be causing any problems
I also have RewriteBase / after my RewriteEngine On line
My rewrite rule looks like this: RewriteRule ^common/(.*)$ common.php?file=$1 [QSA,L], which tells me that your rule should looke like this RewriteRule ^bookmark/(.*) bookmarks-details.php?bid=$1 [QSA,L]
you should use only one RewriteEngine on
RewriteRule /bid/(.*) bookmarks-details.php?bid=$1 - put this line after Options +FollowSymLinks
Try again
Do you want you url to be /bid/55 or /bookmark/55? because you have written it as if it is going to be /bid/55...
Anyway, your .htaccess should look more like this:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^urlbookmarking.com [NC]
RewriteRule ^(.*)$ http://www.urlbookmarking.com/$1 [L,R=301]
RewriteRule ^bid/(.*)$ bookmarks-details.php?bid=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
...without the multiple RewriteEngine on directives (these don't break anything but are unnecessary), and without the leading forward slash on the bid rewrite rule. Also, put your new rule before the rules that rewrites for non-existent file so it doesn't rewrite your URL before you get a chance to use it, and add a [L] flag to the rule so it doesn't get further modified by the other rules. Also, add the line start/end markers (^/$) to the rule.
You would only use the leading forward slash if you were putting the rules in httpd.conf, you don't use them in .htaccess files.
If you want your urls to be /bookmark/, just replace bid with bookmark.
This should redirect all '/bookmarks-details.php\?bid=(id)' urls with bookmarks ids (that have only numbers) to /bookmark/(id).
RewriteRule ^/bookmarks-details\.php\?bid=([0-9]+) /bookmark/$1 [R, NC, L]
Once you successfully rewritten the URL, you then need to write a companion rule to process it, like so:
RewriteRule ^/bookmark/([0-9]+) /bookmarks-details\.php\?bid=$1 [NC, L]
If should go between the rule that always adds 'www' to the beginning and the catch all rule, which I placed at the end. All together, it may look like so:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^urlbookmarking.com [NC]
RewriteRule ^(.*)$ http://www.urlbookmarking.com/$1 [L,R=301]
RewriteRule ^/bookmarks-details\.php\?bid=([0-9]+) /bookmark/$1 [R, NC, L]
RewriteRule ^/bookmark/([0-9]+) /bookmarks-details\.php\?bid=$1 [NC, L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
This link may make things clearer: http://corz.org/serv/tricks/htaccess2.php

Categories