Rewrite URL for specific file - php

I am looking for a way to remove the extension of a file through .htaccess. For example, I want http://example.com/blog.php to be rewritten to http://example.com/blog. BUT I only want blog.php's extension to be removed, not any other file.
I tried this...
RewriteRule ^blog$ blog.php [L]
But it lets http://example.com/blog AND http://example.com/blog.php to be loaded. It doesn't rewrite the URL. \
I mainly want to do this so I don't have to go through all my code, changing all the links going to http://example.com/blog.php to http://example.com/blog

It doesn't rewrite the URL.
Rewriting happens internally within the server. Your rule rewrites the request URI from /blog to /blog.php. It doesn't interact with the browser at all. What you are asking for is an external redirect. You want mod_rewrite to return a redirect to the browser and have the browser send a whole different request. That can be done using:
RewriteCond %{THE_REQUEST} \ /+blog\.php
RewriteRule ^ /blog [L,R=301]

This worked for me :
RewriteCond %{REQUEST_URI} blog
RewriteRule ^(.*)$ blog.php [R=301,L]
Basically, if someone accesses your site using the URL <yoursite>/blog, the .htaccess will rewrite it to <yoursite>/blog.php

Related

Url rewriting and redirrecting not working (quite)

I have a WordPress installation in my main folder, so my site goes like
www.example.com
Besides that I have a subfolder with a separate static site that you can access with
www.example.com/special
So in the special pages I have a page called
www.example.com/special/my-special-page
It's actually a .php file, but I've removed the extensions in the .htaccess file of that special site with this
RewriteEngine On # Turn on the rewriting engine
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
And this works.
But my client said: I want, when I enter
www.example.com/myawardspage
to go to
www.example.com/special/my-special-page
but to have the above URL (without /special/my-special-page) in it. So in the main .htaccess file (the one that controls the WordPress) I've added
RewriteRule ^myawardspage?$ http://www.example.com/special/my-special-page [NC,L]
So now when you go to
www.example.com/myawardspage
I am redirected to
www.example.com/special/my-special-page
which is great, but I need the URL to look like
www.example.com/myawardspage
So in the .htaccess file of the special page I've added
RewriteRule ^myawardspage/?$ /special/my-special-page [NC]
But the URL remains the same (http://www.example.com/special/my-special-page).
What am I doing wrong here?
Just below RewriteEngine on, place the following lines in your /.htaccess (document root) file:
# Check if the file being requested exists
# in the 'special' directory
RewriteCond %{DOCUMENT_ROOT}/special/$1.php -f
# If so, then rewrite accordingly:
RewriteRule ^([^.]+) special/$1.php [L]
Using this method means that you don't need that rule in the special/.htaccess file - you can safely remove it.
Ok, so you mean when ever you want to hit www.example.com/myawardspage then you want to get data from www.example.com/special/my-special-page while in your address bar your address remains www.example.com/myawardspage To achieve this apply this rule in your WordPress installation's .htaccess file.
RewriteRule ^myawardspage/?$ www.example.com/special/my-special-page [NC,L] # Handle requests for "www.example.com/special/my-special-page"
It wil get you desired data and your problem will be solved. Let me know if that works for you.

.htaccess RewriteEngine converting links without changing links

I'm on my begining of learning PHP and very first steps of .htaccess, most of my new web is about main category and few subcategories.
Here are links examples i had before working out .htaccess RewriteEngine:
example.com/index.php?cat=email
example.com/index.php?cat=about&show=some
with help of .htaccess RewriteEngine i've convered them to:
example.com/email/
example.com/about/some/
Here is part of .htaccess:
RewriteRule ^([A-Za-z0-9-]+)/$ index.php?cat=$1 [L]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/$ index.php?cat=$1&show=$2 [L]
RewriteRule ^([A-Za-z0-9-]+)$ index.php?cat=$1 [L]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)$ index.php?cat=$1&show=$2 [L]
Now problem is that most of content have inside links like: "example.com/index.php?cat=about&show=some" Changing them all is option, but anyway... is there anything else could be done? I heard of some .htaccess option that autoconverts links to format you need without changing them manualy, so all links in PHP pages will be the same, but once user gets page loaded, links will be like (example.com/about/some/) Is there anything like that, or is there any other option to leave original link without changing them all?
Cheers!
Links on your site are created with your PHP scripts, Apache with htaccess can't magically change all this links in a raw.
Now what you can do, is redirect old URL to the new ones using htaccess, with a 301 redirection.
For example, if someone click on example.com/index.php?cat=email, Apache will redirect (= the URL will be changed and there will be another HTTP request) to example.com/email/, and then rewrite this to index.php?cat=email
Add this to your htaccess :
RewriteCond %{REQUEST_FILENAME} index.php$
RewriteCond %{QUERY_STRING} cat=([a-zA-Z0-9-]+)
RewriteRule ^ http://example.com/%1/? [L,R=301]
Anyway I strongly recommend you to change the links directly in your code, because even if the solution I've just explained should works (not tested), it's not really healthy to use redirection when you can avoid them.

how to rewrite single URL using .htaccess

I need to rewrite only 1 specific URL, to display to visitors specific content: I tried something like, this:
RewriteEngine on
RewriteCond %{REQUEST_URI} example.com/test/2_5/page.html
RewriteRule ^(.*)$ example.com/tt.html [R,L]
I need to rewrite all requests to:
http://example.com/test/2_5/page.html
to
http://example.com/tt.html
how to do this?
thanks,
Redirect /test/2_5/page.html /tt.html
Ok, mod_rewrite
RewriteRule ^/test/2_5/page.html /tt.html [L]
Remove first / if using .htaccess in the site's root folder, not the .conf file. And, typically, the final url should be the full one, with http:// and domain, but this one will work too. If you want to do everything by the rules then
RewriteRule ^/test/2_5/page\.html$ http://example.com/tt.html [L]

Rewrite url which redirects to page on webserver

I am trying to get a page with a query string to redirect to a nicer looking url then get that url and transfer it back to the original query string but without redirecting (i.e. without changing the url)
At the moment I am getting a redirect loop (for obvious reasons) but I was hoping for a way to stop this.
This is my code in my htaccess file
#rewrite search querystring
#/search/'apartment'/2_bedrooms/price_0-500000/town_W4/development_18,SW5/
RewriteRule ^search/([^/]+)/([^/]+)_bedrooms/price_([^/]+)-([^/]+)/town_([^/]+)/development_([^/]+) /search.php?propertytype=$1&bedrooms=$2&minprice=$3&maxprice=$4&location=$5&development=$6 [NC]
RewriteCond %{QUERY_STRING} propertytype=([^/]+)&bedrooms=([^/]+)&minprice=([^/]+)&maxprice=([^/]+)&location=([^/]+)&development=([^/]+)
/search/$1/$2_bedrooms/price_$3-$4/town_$5/development_$6 [R,L]
RewriteRule ^(.*)$ /search/%1/%2_bedrooms/price_%3-%4/town_%5/development_%6? [R,L]
So what it is meant to do is:
user has been taken to:
http://www.domain.com/search/?propertytype=dev&bedrooms=2&minprice=0&maxprice=10000000&location=W1&development=W1
This page is the actual page on the server where the data is coming from, however I want the user to see.
http://www.domain.com/search/dev/2_bedrooms/price_0-10000000/town_W1/development_W1/
Is it possible to do this without a redirect loop.
Thanks for your help
EDIT I'm thinking it could be done with the rewrite flags but I'm not sure, I'm quite new to the Rewrite Engine
Edited:
Here is a complete (and working) solution for you:
RewriteEngine On
# User gets here:
# http://localhost/search/?propertytype=dev&bedrooms=2&minprice=0&maxprice=10000000&location=W1&development=W1
# He is explicit redirected to here:
# http://localhost/search/dev/2_bedrooms/price_0-10000000/town_W1/development_W1/
# Internally, apache calls this:
# http://localhost/search.php?propertytype=dev&bedrooms=2&minprice=0&maxprice=10000000&location=W1&development=W1
RewriteRule ^search/([^/]+)/([^/]+)_bedrooms/price_([^/]+)-([^/]+)/town_([^/]+)/development_([^/]+) search.php?propertytype=$1&bedrooms=$2&minprice=$3&maxprice=$4&location=$5&development=$6 [NC,PT]
RewriteCond %{QUERY_STRING} propertytype=([^/]+)&bedrooms=([^/]+)&minprice=([^/]+)&maxprice=([^/]+)&location=([^/]+)&development=([^/]+)
RewriteRule ^search/(.*)$ /search/%1/%2_bedrooms/price_%3-%4/town_%5/development_%6/? [R,L]
It assumes you put .htaccess in server root and that there is a file search.php in root too.
Original:
I think you can use PT and QSA Rewrite Rule flags (http://httpd.apache.org/docs/current/rewrite/flags.html) in your first rule
Use PT for server-side redirection (it will not change the URL for the user/browser, but will for your server-side scripts)
Use QSA if you wanna carry the query while doing this redirection
You can redirect all requests that don't target an existing file to a specific php-script, for example:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [PT,QSA]

Redirecting Certain Requests into Subdir with htaccess

I'm launching a new site on WordPress but would like all of my old links to remain active. To make this possible I've moved my present site into a subdirectory (/oldsite) and installed the new wordpress site directly in root.
How would I go about redirectin any url that matches the old path style into the /oldsite directory so that:
http://example.com/stories/read/4231/some-story-title
is automatically redirected to
http://example.com/oldsite/stories/read/4231/some-story-title
The same would go for many other domain patterns like:
http://example.com/exclusives becoming http://example.com/oldsite/exclusives and a few more.
I don't want all requests to go into the /oldsite directory, since I will rely onmy WordPress instance in root.
.htaccess is still somewhat cryptic to me, so I would appreciate any direction.
Something like the following should work.
RewriteCond %{REQUEST_URI} ^/(stories|exclusives)/ [NC]
RewriteRule .* /oldsite/%1 [R=301,L]
Try this one
RewriteCond %{HTTP_HOST} ^(example.com)$ [NC]
RewriteRule ^(.*)$ /oldsite/%2%{REQUEST_URI} [QSA,L]
It will not actually redirect .. It will internally run from oldsite folder
If u want to redirect then add [R=301,L] to RewriteRule

Categories