How To Rewrite URL in .htaccess? - php

I have a site/blog that pages URL as www.example.com/sample-page and I want to make it like www.example.com/p/sample-page.html For this purpose I am thinking to use .htaccess file to write rewrite/redirect rule to do it. So can we make it using .htaccess?

You probably should be looking at the mod_rewrite documentation for apache (the rules in there can be placed into a .htaccess file).
http://httpd.apache.org/docs/current/rewrite/remapping.html
However, for your specific case (which I think is to end up mapping the real URL www.example.com/WP/p/sample-page.html to www.example.com/WP/sample-page), you could do something like this:
RewriteEngine on
RewriteRule ^WP/sample-page$ WP/p/sample-page.html [NC]

Related

How to change a URL parameter to /parameter

I have a URL that looks like this: https://www.example.com/users.php?username=Name. Instead of this I want it to look like this: https://www.example.com/users/username=Name. What are the steps to aquire this? I have tried searching, but I just cannot figure out what to search to find the answer.
It depends if you're using an apache server you can acquire this with .htaccess, in that case you could use RewriteEngine On with the specific RewriteRule.
.htaccess file in the root of you're website/domain:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^users/username=(.*)$ /users.php?username=$1
Beside this there are a lot of more options and flags with htaccess rewriterule
If you're using nginx it will be done on the nginx way, you may use an converter from htaccess to nginx of fins some more information for nginx rewrite url.

Go to file by link

I store data in text file.
And when user enter in address bar something like
my_syte.com/aaa - (without extension)- I need to file_get_contents aaa.txt file
my_syte.com/bbb - I need to file_get_contents bbb.txt file
Please advise the most powerful way of do it. Apache server.
Thanks
On Apache servers you can use mod-rewrite in .htaccess file:
RewriteEngine on
RewriteRule ^([a-zA-Z]+)$ /$1.txt [L]
if your files can contain - or _ or numbers then use:
RewriteEngine on
RewriteRule ^([a-zA-Z0-9_-]+)$ /$1.txt [L]
On nginx servers it's more complicated but some of them works with .htaccess. On other servers there may be entirely different approach. It's hard to help you without more informations.
As you said it's Apache, then use examples above. Either edit or create .htaccess file on your webroot (directory which is accessed by domain). First check if it were there (could be hidden) and if it exists then only edit it (add lines at the top).
If it doesn't exist, then create one by yourself.
Can you please give us some insights about your server? Apache nginx?
In Apache, you can achieve that with url rewriting.
Enable mod_rewrite in apache
Put the following line of code in .htaccess on the same location of my_site.com/
RewriteEngine on
RewriteRule ^/foo$ /foo.txt [PT]
to make it generic
RewriteEngine on
RewriteRule ^/*$ /foo.txt [PT]
Maybe I am wrong in sytax based on your specific server configuration. You need to make the best possible regular expression for this case.

Rewriting a URL to a PHP parameter from inside a subdirectory

What I'm asking for is simply rewriting any URL-given sub-directiories to a PHP URL parameter for nicer looking, user friendly URL.
I know that this is possible using the Apache Rewrite Engine and associated parameters.
The problem on my end is that I do not want to rewrite i.e
mydomain.com/parameter ----------->
mydomain.com/index.php?id=parameter
But rewriting
mydomain.com/subdirectory/parameter ----------->
mydomain.com/subdirectory/index.php?id=parameter
In my root folder I have an .htaccess file for 404 errors and such, redirecting non-existant pages to the main one. During the tests I've done I have deactivated these thinking that they could overwrite the other .htaccess file.
I tried...
RewriteEngine On
RewriteRule ^directory/([^/\.]+)/?$ index.php?id=$1 [L]
...and other snippets similar to that one.
The .htaccess file containing this code was placed in the root/directory folder.
What I wanted to achieve with that was to redirect root/directory/string to root/directory/index.php?id=string.
Since my knowledge of .htaccess and rewriting is obviously limited, I need answers to two questions.
Will a 404 rewriter in the root folder overwrite any other rewriter in a subdirectory?
How do I achieve what I'm after? (much like how url-shorteners work - bit.ly/shortened) from a subdirectory?
This rule should work for you in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteRule ^(directory)/([^/.]+)/?$ /$1/index.php?id=$2 [L,NC,QSA]
Make sure there is no other .htaccess file in directory.

.htaccess remove extension while preserving uri parameters

i was trying to rewrite my urls in a friendly way; i have written all the php code to handle everything.
The only thing that i am unable do is removing the .php extension while preserving the uri parameter.
So, essentially, i'm looking for some .htaccess rules to change from this:
www.example.com/biography.php/john-doe
to this:
www.example.com/biography/john-doe
(john-doe isn't a real file, it's only the GET parameter passed to biography.php)
Thanks everyone.
If the links on your site are already like this
www.example.com/biography/john-doe
and you want to process them on the server like this
www.example.com/biography.php/john-doe
add the following to the .htaccess file in the root directory of your site.
RewriteEngine on
RewriteBase /
RewriteRule ^([-a-zA-Z]+)/([-a-zA-Z]+)$ $1.php/$2 [L]
Should do:
RewriteRule ([^\.]).php(.*) $1$2 [QSA,R=301]

Search-Engine Friendly URLs

I am working on building my first search-engine friendly CMS. I know that perhaps one of the biggest keys to having and SEO site is to have search-engine friendly URLs. So having a link like this:
http://www.mysite.com/product/details/page1
will result in much better rankings than one like this:
http://www.mysite.com/index.php?pageID=37
I know that to create URLs like the first one, I have one of two options:
use a web technology, in this case PHP, to create a directory structure
leverage Apache's mod_rewrite add-on to have these URLs passed to a PHP processor
As far as the PHP goes, I'm pretty comfortable with anything. However, I think the first option would be more difficult to maintain.
Could someone show me how to write an .htaccess file, which will:
silently direct SEO URLs to a processor script
not redirect if the requested URL is an actual directory on the server
Is there a better way than the way I am trying it?
You can use .htaccess for apache, create file in your root folder of web mainly "htdocs" name it ".htaccess" add next content to it
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
Options -Indexes
</IfModule>
in your php file you can access data from $_GET
$_GET['url'];
Then you can use data to parse what you need.
Yes, the first option would be pretty hard to maintain. If you want to change the header of the pages, you'd need to recalculate all of the pages.
The simplest way to do that would be to have a PHP file named product.php or product/details.php and use the $_SERVER\['PATH_INFO'\] variable to figure out what the client requested.

Categories