I am trying to work with pretty url's. but it will go to 404.php
I don't know what is wrong with my .htaccess . I'm new with this prettyurl
.htaccess
RewriteEngine on
RewriteRule ^([0-9]+)/$ test.php?id=$1
# not a file
RewriteCond %{REQUEST_FILENAME} !-f
# not a directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .? /404.php [L]
in my test.php
here is the only code inside in my test.php
if(isset($_GET['id'])){
$id = $_GET['id'];
echo $id;
}
but it's not printing.
I point my browser like this
http://localhost/myprojectfolder/test/1
structure of my project
myporjectfolder
- index.php
- .htaccess
- test.php
Thank you in advance.
I think you have a couple of issues. You are pointing to http://localhost/myprojectfolder/test/1 but you don't have /test in your rule. You have it only accepting digits only. Also you are not using a slash at the end of the URL but your rule says it should be there so that won't match either, unless you make it optional.
Also you are directing non existent files to 404.php. Well making pretty URL's which you are trying to do are non existent files as well. So that could cause problems.
Anyway, Try your rules this way for a URL like this
http://localhost/myprojectfolder/test/1
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^test/([0-9]+)/?$ test.php?id=$1 [L]
Or remove the /test folder which seems unnecessary anyway
http://localhost/myprojectfolder/1
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+)/?$ test.php?id=$1 [L]
Related
I have a webpage with url website.com/app/ there is an index.php in the dir and also an .htaccess file.
I want a make this url into a clean url
website.com/app/?app=app1&id=12345
to
website.com/app/app1/12345
I was able to achieve this with this rule
RewriteEngine On
# Don't match real existing files so CSS, scripts, images aren't rewritten
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+) index.php?app=$1&id=$2 [L]
RewriteRule ^([^/]+)index.php?app=$1 [L]
but I also want to be able to access the webpage with this url
website.com/app/app1
so that It'll mean website.com/app/?app=app1 But currently I get "Object not found!" when trying this.
Thank you!
Try this RewriteRule, it will leave you with the URL: website.com/app/app1
RewriteEngine On
RewriteRule ^app/([^/]*)$ /app/?app=$1 [L]
I'm answering my own qn here :
RewriteEngine On
# Don't match real existing files so CSS, scripts, images aren't rewritten
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)$ /apps/?app=$1 [L]
RewriteRule ^([^/]*)/([^/]*)$ index.php?app=$1&id=$2 [L]
You're welcome
I have this url:
www.mywebsite.com/news/best-ever-phones
The part best-ever-phones is variable and could be anything.
index.php file is checking database for article with url best-ever-phones
The problem is that in ./news/.htaccess file I have this code:
RewriteEngine On
RewriteBase /news/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ index.php?article=$1 [L,QSA]
which is supposed to load index.php file but server returns error 404 Not found when I visit www.mywebsite.com/news/best-ever-phones url.
What is wrong?
Try this
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?article=$1 [L]
I think the problem may be with Rewrite base and your relative path. But this is how i'ld do it.
<IfModule mod_rewrite.c>
Options -Indexes
RewriteEngine On
RewriteRule ^news/([A-Za-z-.]+)/?$ news.php?article=$1 [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]
</IfModule>
This should fix you up, rename your index to news.php
Perhaps give this a shot:
RewriteEngine On
RewriteBase /news/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-\/]+)\/?$ index.php?article=$1 [L,QSA]
This is presuming that you want the / character to appear in your $article variable, so you can logically separate different parts of the friendly URI if you want to in /news/index.php. Otherwise, single segment friendly URIs like /news-item-1 or whatnot can end with a trailing slash or not.
Really you don't want trailing slashes, as it might result in duplicate content penalties for loading the same page with a different URL (the / counts), so you could either not match \/? outside of the capture group or 301 redirect in index.php to the URL with the trailing slash removed. This way you also have clean query arguments, e.g. /news/something-big?orly=yes instead of /news/something-big/?orly=yes
I am using this site to test the rewrites. Otherwise untested as we don't have your code handy, so no way to tell whether it would work or not in your environment.
This code is used when we click on any url it will hide its extension.
But If you want to goto index.php page then write
echo 'Index Page ';
?>
I am building a clean url system using .htaccess and php.
The main idea is to rewrite everything to index.php and to split the url to segments separated by /.
It works but when I upload it to web server I get an infinite loop error.
My .htaccess file is:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
If I get things correctly - the problem is that it redirects index.php to itself but it shouldn't because I have index.php file that handles everything and RewriteCond %{REQUEST_FILENAME} !-f which should prevent that rewrite.
Could you please help me with this .htaccess - I want it to rewrite everything to index.php and to be as portable as it can be, so I can use it in:
www.mysite.com
www.myothersite.com
or even
www.mysite.com/new/
I also want to redirect something like www.mysite.com/articles/2 to index.php as well as www.mysite.com/articles/it/2 or even www.mysite.com/articles/it/search/searchterm
Here's code taken from Drupal's .htaccess which uses the "clean urls" approach you are trying to achieve.
# Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
In this case, it appends the clean URL as the "q" querystring parameter. So then your script can detect what content to deliver based on $_GET['q'].
And here's another approach, this one from Wordpress.
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
This one specifically looks for requests to index.php and filters them out before doing the redirection.
My question might be dumb, but I googled it and didn't find an answer...
Let's say I want to access my website in this given url: www.mywebsite.com/something/something_else/?some_query_string=true
(I put the query string because it is going to be there, and I don't know if it makes any difference in the htaccess file)
I want it to keep the URL the same, but load the index file for no matter what URL, which is not in the root of the server.
My server has an "application" folder, where all the code is.
How can I do this?
Thanks!
use htaccess to re-write all request to index.php (except for when a file/dir/link is requested and it exists):
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php [L]
</IfModule>
If you want to use Rewrite to have your requests handled by a file outside the DocumentRoot, then you can combine with an Alias directive.
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* application/index.php [PT]
Alias application /path/to/application
Note the [PT] on the RewriteRule which means 'pass through' - it ensures the rewritten url is passed through to other apache modules which might be interesting in processing it.
This turned out to answer my own question:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ application/index.php?url=$1 [QSA,L]
If the URL doesn't exist, it loads the index.php file inside of the folder "application", and it keeps the URL the same, which was exactly what I needed...
Thanks for the answers!
can anybody please tell me the rewrite rule so I can make a URL like this:
http://www.mysite.com/index.php?page=directory1/page
what I'm trying to do is remove the "index.php?page=" so i get a
"http://www.mysite.com/directory1/page"
This is how Drupal does it; it's very similar to toneplex's example, but it doesn't mess around with extension checking and it adds an extra check on favicon.ico, which many browsers automatically request; this saves an extra hit on your PHP code if you're missing favicon.ico.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^ index.php [L]
I'm assuming you are trying to bootstrap everything via the index.php file. So try this out. Any file or directory that doesn't exist will be force through the index.php file.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !\.(js|ico|gif|jpg|png|css)$ /index.php