I have two websites, xyz.com and testsite.com (just for example). I've linked my domain xyz.com to testsite.com/content. My testsite.com/content features a .htaccess which features the following code meant to remove the .php extension from the URL:
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
It works perfectly fine on the source website; however, it shoots off a 404 error in the xyz.com and references /content/content/login instead of /content/login in the testsite.com. How do I fix this?
Add RewriteBase / to the code so like this
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
If it doesn't work like this, try adding the rewritebase under the options or at the top of the htaccess.
Related
I attempted to create a little bit of htaccess which alters a URL from something like
http://localhost/website/page.php?id=_abc-123
to
http://localhost/website/page/_abc-123
It works for the most part, in that I can visit the page without having trouble locating scripts and CSS files. However, if I try to echo out $_GET["id"], instead of getting _abc-123, I will get _abc-123.php.
This is what I have so far within my htaccess file:
Options +FollowSymLinks
# remove extensions
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
# movie page
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^page/([^/]*)$ page.php?id=$1 [L]
All help is appreciated,
Thanks.
Test movie page first, and test if file with .php exists:
Options +FollowSymLinks -MultiViews
RewriteEngine On
# movie page
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^page/([^/]*)$ page.php?id=$1 [L]
# remove extensions
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^.]+)/?$ $1.php [L]
First of all, if you are not sure where your error lies, you can try online tools for .htaccess like htaccess.mwl.be.
Obviously your first RewriteRule contitions are met, which results in your "error".
With the help of this tool and some knowledge about how regex work, we can fix your .htaccess to this:
Options +FollowSymLinks
# remove extensions
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC]
# movie page
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^page/([^/]*)$ page.php?id=$1 [L]
The only thing I changed is removing the "L" flag from your first RewriteRule, because its RewriteCond is met but we need it to go through the second RewriteRule.
For more information about the L-flag have a look at the documentation.
I'm trying to build a shopping cart using PHP & MySQL. Right now I'm in the process of creating product-detail pages. So this is how I set up my htaccess (located in the root folder):
Options +FollowSymLinks
Options -Indexes
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
My problem is I can only access a certain page if index.php is included. For example, http://localhost/thegamingplace/products/details/1 displays a "Not Found" error, but http://localhost/thegamingplace/index.php/products/details/1 works.
Can someone take a look at my htaccess and tell me what I'm doing wrong?
Try this:
RewriteCond %{REQUEST_URI} !^(index\.php|resources|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ index.php/$1 [NC,L]
It works when tested at http://htaccess.mwl.be/
If it doesn't work make sure you have mod_rewrite installed and enabled.
Hi I have the following redirection for a friendly URL site
Options +FollowSymLinks -Multiviews
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
rewritecond %{SCRIPT_FILENAME} !-d
rewritecond %{SCRIPT_FILENAME} !-f
rewriterule ^(.*)$ $1.php [L]
rewriterule ^noticia/([a-zA-Z0-9_-]+)$ noticia.php?title=$1 [L]
</IfModule>
If i go to www.mysite.com/noticias or any other URL like that everything goes ok
But if i try to go to www.mysite.com/noticia/this-is-a-parameter
When I access a link I do it like <a href="noticia/this-is-a-parameter">
I get Internal Server Error message and white screen
In localhost it was working perfectly, but can't find a solution in the server.
Any help would be appreciated
Check for presence of .php file before adding php extension and reorder your rules:
Options +FollowSymLinks -Multiviews
RewriteEngine on
RewriteBase /
RewriteRule ^noticia/([\w-]+)/?$ noticia.php?title=$1 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
Rewrite rule does not work on remote host in subdirectory.
While in production phase, I had this directory http://localhost/prj/. I used RewriteRule to "hide" and "load" PHP files without extension. This is the .htaccess I've used:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
So, that worked fine, I've developed the project and I wanted to upload it to my VPS for remote use and such. The thing is, the project is still in beta, and I don't keep it in root directory ATM but at subdirectory /beta/, so now, this my URL right now is like this: http://example.com/beta/.
Yet when I tried to access that URL, the index.php is automatically loaded, but when I access for example file play.php as http://example.com/beta/play it doesn't work but it worked while the project was in production.
This is what I have tried:
To use RewriteBase to /beta/
RewriteBase /beta/
To use different options
Options All -Indexes -MultiViews
To use directory before actual RewriteRule
RewriteRule ^/beta/(.*)$ $1.php
To use operatives [L, QSA]
Example:
Options All -Indexes -MultiViews
RewriteEngine on
RewriteBase /beta/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L,QSA]
Options All -Indexes -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/beta/(.*)$ $1.php [L,QSA]
Returned error is 404 Not found.
Edit: I'm I supposed to have too inside apache2.conf?
Try this in /beta/.htaccess:
Options All -Indexes -MultiViews
RewriteEngine on
RewriteBase /beta/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/beta/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
I have been working on localhost, and my htaccess file is
Options +FollowSymLinks
Options -Indexes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) $1.php [L]
after adding the htacces code,the url
localhost/movies/news.php
works
localhost/movies/news
also works but
localhost/movies/news/
doesn't work. It shows "Internal Server Error".How to make it work with slash and without slash.
You an try this code:
Options +FollowSymLinks -MultiViews
RewriteEngine On
# Internally forwards movies/news/ to movies/news.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
The problem is when you add the slash you have news/.php and this is not working.
A better solution is to rewrite to a GET variable something like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) index.php?url=$1 [L]
Then you can filter the GET variable in your script and include the file or content you need.