Cant get .htaccess to work proper - php

i am doing a URL-Shortener where i am trying to do it URL friendly.. Currently my adress is typed like this "example.com/index.php?name=sitename" i want it do be like "example.com/sitename".. I guess i could use some cool rewrite rule but i cant get it to work..
I did something like this but it wont work
Options All -Indexes
DirectoryIndex index.php index.html index.htm
RewriteEngine On
RewriteRule ^([^/]*)/$ /index.php?name=$1 [L]
but i dont know :s?

Here is the code you need to place in DOCUMENT_ROOT/.htaccess
Options All +FollowSymLinks -MultiViews -Indexes
DirectoryIndex index.php index.html index.htm
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?name=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ /index.php?name=$1 [L,QSA]
Reference: Apache mod_rewrite Introduction

Your regular expression will fail on URLs with more than one / after the domain, because [^/] stops as soon as it sees one of these characters, and then it looks for the last slash with /$, which is not always the case.
Assuming you want to convert these URLs
example.com/HiThere
example.com/Your/Page
to:
example.com/index.php?name=HiThere
example.com/index.php?name=Your/Page
Try this rewrite rule instead:
RewriteRule ^(.*?)/$ /index.php?name=$1 [L]
This will look for all characters with (.*?) UNTIL it reaches the last /. I'm not 100% sure about the [L] flag, though.

the simplest method is to use 404 error to point to index.php and then get the sitename using php
.htaccess file
ErrorDocument 404 /index.php
PHP CODE
<?php
$sitename=substr(strrchr($_SERVER["REQUEST_URI"],"/"),"1");
now simply visit 'example.com/sitename' and sitename will be stored in $sitename.
And if($sitename==""){//Show your HomePage}
Hope it helps!!

Related

I can't change my streaming website homepage

I'm trying to change the homepage of my site but nothing is happening. When entering the code below "DirectoryIndex index.html #to make index.html as index" my home page remains the same. I'm making changes to the file in .htaccess
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
# Explicitly disable rewriting for front controllers
RewriteRule ^index.php - [L]
RewriteRule ^index.php - [L]
RewriteCond %{REQUEST_FILENAME} !-f
# Change below before deploying to production
#RewriteRule ^(.*)$ index.php [QSA,L]
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
DirectoryIndex index.html #to make index.html as index
My website is written in php and I want to put the homepage in html
RewriteRule ^(.*)$ index.php [QSA,L]
Change the * quantifier to +, so that it only matches non-empty URL-paths, to allow the DirectoryIndex document (ie. index.html) to be served for requests to the homepage, instead of passing the request through to the front-controller (ie. index.php). Or, simply use a dot (.) as the regex since you aren't doing anything with the captured URL-path. For example:
RewriteRule . index.php [L]
(The QSA flag is not required here.)
Although, since you are using a front-controller pattern (ie. routing all requests to index.php), you should probably be configuring the appropriate response to be served from index.php instead?
Aside:
DirectoryIndex index.html #to make index.html as index
You should remove, what you think is a comment at the end of the line, ie. "#to make index.html as index". That's not actually a comment. Apache does not support line-end comments (only full-line comments are supported). In this instance, #to, make, index.html, as and index will be seen as additional arguments to the DirectoryIndex directive (so you don't actually get an error).
See my answer to the following question regarding the use of line-end comments:
Error 500: .htaccess RewriteCond: bad flag delimiters
UPDATE:
Alternative solution
Try the following instead (this replaces the entire .htaccess file above):
Options +FollowSymlinks
RewriteEngine On
# Explicitly disable rewriting for front controllers
RewriteRule ^index\.(php|html)$ - [L]
# Rewrite any request for anything that is not a file to "index.php"
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
# Rewrite the homepage only to "index.html"
RewriteRule ^$ index.html [L]
The <IfModule> wrapper is not required. (This is your server so you know if mod_rewrite is enabled or not and these directives are not optional.)
Make sure you've cleared your browser cache before testing.

removing /index from the url on htaccess

My current urls look like this [mysite].com/index
I removed .php's with the following .htaccess code but i can not remove the index from the url.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
</IfModule>
How can i remove 'index' from the url?
EDIT
I found why it does not work because of windows hostings does not support htaccess method.
Use
DirectoryIndex index.php
Now apache will treat index.php as directory index and you don't have to type index or index.php in url bar.
Actually, removing the page name for good it's not a good practice.
It may cause errors, and it's not good for SEO.
I strong suggest you to use friendly URL to remove the PHP extension(the way you're doing right now) or put some other name more friendly.
Exemple:
RewriteEngine On
RewriteRule ^home/?$ /my_new_index.php [NC,L]
RewriteRule ^new-order/?$ /new_order_from_client.php [NC,L]
It's the best practice!

Htaccess new line crashes page

I recently tried to hide parameters from my url, using htaccess, ended up with a 500 Server Error.
Here is my .htaccess code:
<files .htaccess>
order allow,deny
deny from all
</files>
Options -Indexes +FollowSymLinks
RewriteEngine on
RewriteBase /
ErrorDocument 404 /404.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteRule ^(.+?)/$ /$1 [R=301,L]
RewriteBase /
RewriteRule ^([^/]+\.php)/.* $1 [R=301,L]
RewriteRule ^buyit\.php$ /buyit.php?qstart=1&gid=73161-645821-36&qid=4&daq=0&preq=14&clk=7 [L]
All I wanted to do is my page buyit.php?qsta=1&gid=11-64-36&qid=4&kaq=0&req=14&dlk=7 to show always as buyit.php keeping my variables intact and usuable for my php scripts. So I added this last line in htaccess but unfortunately it crashes.
Any ideas why, please?
Your code is looping. This is because you can't match the query string in a rewrite rule, thus a URL like:
/buyit.php?qstart=1&gid=73161-645821-36&qid=4&daq=0&preq=14&clk=7
actually matches:
^buyit\.php$
So you need to add a condition to check that there isn't a query string. Change your last rule to this:
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^buyit\.php$ /buyit.php?qstart=1&gid=73161-645821-36&qid=4&daq=0&preq=14&clk=7 [L]
Unfortunately, if you are passing values using GET method, you cant simply get rid off the parameters. However, you can remove the parameters name and make the url unique and NOT EASILY UNDERSTANDABLE by some one who doesn't know about the parameter names.
Try this htaccess code.
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)\.html$ /buyit.php?qsta=$1&gid=$2&qid=$3&kaq=$4&req=$5&dlk=$6 [L]
It converts your URL from this :
http://yourdomain.com/buyit.php?qsta=1&gid=11-64-36&qid=4&kaq=0&req=14&dlk=7
to this
http://ydomain.com/1/11-64-36/4/0/14/7.html
which is also good for your SEO.

RewriteRule for all .php pages with constant querystring

I'm working on a project which all pages need to have a querystring assumed id. for example following pages:
example.com/alfa.php?id=1
example.com/beta.php?id=30
exapmle.com/gamma.php?id=50
I'm using the following .htaccess to make my URLs beautiful.
RewriteEngine On
## BEAUTIFUL URL
DirectoryIndex default.php
Options -Multiviews -Indexes +FollowSymLinks
#RewriteBase /
DirectorySlash off
# remove trailing slash
RewriteRule ^(.*)\/(\?.*)?$ $1$2 [R=301,L]
# rewrite /dir/file?query to /dir/file.php?query
RewriteRule ^([\w\/-]+)(\?.*)?$ $1.php$2 [L,T=application/x-httpd-php]
This code will make the following convertion:
example.com/alfa.php?id=1 to example.com/alfa?id=1
Assuming that X.php is everytime changing (X) but id is always constant, What is the best content of .htaccess file?
I tried several methods, But what is the best?
and one thing more, How can I convert URL into example.com/alfa/1 AND example.com/beta/30
You're trying to match against the query string (all the stuff after ?) in the regex pattern of your rule. The query string isn't part of that match, only the URI. But since you don't want the query string to be touched, just don't match against it since it'll get appended automatically:
RewriteEngine On
## BEAUTIFUL URL
DirectoryIndex default.php
Options -Multiviews -Indexes +FollowSymLinks
#RewriteBase /
DirectorySlash off
# remove trailing slash
RewriteRule ^(.*)\/$ $1 [R=301,L]
# rewrite /dir/file?query to /dir/file.php?query
RewriteRule ^([\w/-]+)$ $1.php [L,T=application/x-httpd-php]
For example.com/alfa/1 and example.com/beta/30:
RewriteCond %{THE_REQUEST} [A-Z]{3,9}\ /([\w/-]+)(\.php)?\?id=([0-9]+)
RewriteRule ^ /%1/%3 [L,R=301]
RewriteRule ^([\w/-]+?)/([0-9]+)$ $1.php?id=$2 [L,QSA,T=application/x-httpd-php]
Something else you can try doing is getting rid of your rules and just using Multiviews, which you have turned off. Multiviews and mod_negotiation was made for stuff like this.

how to rewrite a url with two variables using .htaccess

i want rewrite it with a .htaccess i have this url:
../index.php?page=details&id=123456
like this:
../detail/123456
but I do not really know how to do this. Could you help me to rewrite this url?
or give me a simple example that I can understand how it works
RewriteRule ^(.*)/([0-9]+)$ index.php?page=$1&id=$2&%{QUERY_STRING}
The first (.*) selects any string, e.g. "details". ([0-9]+) catches a number, in your case "123456"
Finally
&%{QUERY_STRING}
ensures, that additional parameters are added to your backendscript, too.
This should work:
RewriteEngine On
RewriteRule ^detail/([^/]*)$ /index.php?page=details&id=$1 [L]
Here is an example I use for my webpage when it's in a subdirectory.
# "learn/degree/index.php?d=mathematics" shows as "learn/degree/mathematics"
RewriteRule ^learn/degree/([a-zA-Z_-]+)$ learn/degree/index.php?d=$1
To complete it, remember to write at the beginning of the page this:
RewriteBase /
RewriteEngine On
Here's the 'full' .htaccess I use in my page to give you some idea.
#Redirect any www. to the page without it. Helps to keep user logged.
RewriteBase /
RewriteEngine On
rewriteCond %{HTTP_HOST} ^www.newfutureuniversity.org [NC]
rewriteRule ^(.*)$ http://newfutureuniversity.org/$1 [R=301,L]
Options +Indexes
Options +FollowSymlinks
# Finally working. Rewrite the user so "/student/Username" will internally be "/student/?user=Username"
RewriteRule ^student/([a-zA-Z0-9_-]+)$ student/index.php?user=$1
# Rewrite the disciplines so "learn/degree/1" will internally be "learn/degree/index.php?dis=1"
RewriteRule ^learn/degree/([1-4])$ learn/degree/index.php?dis=$1
# Rewrite the degrees so "learn/degree/mathematics" will internally be "learn/degree/index.php?d=mathematics"
RewriteRule ^learn/degree/([a-zA-Z_-]+)$ learn/degree/index.php?d=$1
# Rewrite the degrees so "learn/subject/2/5/7" will internally be "learn/subject/index.php?class=2&div=5&section=7"
RewriteRule ^learn/subject/([0-9])$ learn/subject/index.php?class=$1
RewriteRule ^learn/subject/([0-9])/([0-9])$ learn/subject/index.php?class=$1&div=$2
RewriteRule ^learn/subject/([0-9])/([0-9])/([0-9])$ learn/subject/index.php?class=$1&div=$2&section=$3
You have to make the links to ../detail/123456, then the script interpretes it internally as ../index.php?page=details&id=123456. If you don't want this, then you are looking for a redirect.
Try this
<IfModule mod_rewrite.c>
RewriteEngine on
Options +FollowSymlinks
# RewriteBase / add this if necessery, commented intentionally
RewriteRule ^detail/([0-9]+)$ index.php?page=details&id=$2
</IfModule>

Categories