A few weeks ago I was trying url rewriting on my shared host. It was working for a while, but nowadays it seems to be broke. I don't know what the problem is and I can't solve it by myself. Hope you could help, guys.
This is the plan. I want to delete the extension in the url (e.g. 'file.html' or 'file.php') to only the filename (e.g. 'file'). My code is the following:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^index/(.*)/$ index.php?page=$1
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([^\.]+)$ $1.html [NC,L]
ErrorDocument 404 404.php
ErrorDocument 403 403.php
ErrorDocument 401 401.php
</IfModule>
One extended note: this .htcaccess file is in the directory 'portfolio'. The root has another .htcaccess file which is the following:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>
Maybe it is something obvious, but I couldn't find what the problem is. I did try 'RewriteBase /website/', 'RewriteBase /', 'RewriteCond %{REQUEST_FILENAME} !-f' and 'RewriteCond %{REQUEST_FILENAME} !-d' in the first .htaccess file, but that doesn't work.
You can have these rules in /portfolio/.htaccess
ErrorDocument 404 404.php
ErrorDocument 403 403.php
ErrorDocument 401 401.php
RewriteEngine On
RewriteBase /portfolio/
RewriteRule ^index/(.+?)/$ index.php?page=$1 [L,QSA]
RewriteCond %{DOCUMENT_ROOT}/portfolio/$1\.html -f [NC]
RewriteRule ^([^.]+)$ $1.html [L]
RewriteRule ^([^.]+)$ $1.php [L]
Your rules for adding .php or .html need to have a RewriteCond to check whether corresponding files exist in your portfolio directory.
Related
How can I manipulate the url by using .htaccess to make the url below work and shorten the size?
I successfully removed the .php, .html extension from the url, but don't have a clue on how to manipulate the character positions and remove the query variable name.
Example:
www.website.com/blog?language=en-us
to:
www.website.com/en-us/blog
RewriteEngine On
DirectoryIndex index.php
ErrorDocument 404 /public_html/404.php
ErrorDocument 500 /public_html/500.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([^\.]+)$ $1.html [NC,L]
With your shown samples, please try following htaccess rules file. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##External redirect 301.
RewriteCond %{THE_REQUEST} \s/(blog)\?language=(\S+)\s [NC]
RewriteRule ^ /%1/%2? [R=301,L]
##Internal rewrite to index.php file.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/(.*)/?$ index.php?param1=$1&language=$2 [QSA,NC,L]
I would appreciate if someone can help with this mod rewrite issue. For years I had this in my htaccess, apache include files and it worked just fine.
RewriteRule ^/article/([0-9]*).html$ /article/article.php?id=$1 [NC,L]
But recently it stopped working. Browser shows "No input file specified." and access log shows 404 response code the requests that had to be fulfilled by the condition above.
RewriteEngine is on. This is what my htaccess mod_rewrite rules at the moment
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \.(js|css|jpeg|jpg|gif|png|ico|map)(\?|$) /404error.php [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteRule ^/article/([0-9]*).html$ /article/article.php?id=$1 [NC,L]
RewriteRule ^/relationships/([0-9]*).html$ /article/article.php?id=$1 [NC,L]
</IfModule>
With your shown samples/attempts, could you please try following htaccess Rules. Please make sure to place your htaccess rules file in root and clear your browser cache before testing your URLs.
<IfModule mod_rewrite.c>
Options -MultiViews -Indexes
RewriteEngine On
RewriteBase /
##Rules for non-existing pages.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
##Rules for articile and relationship uris here.
RewriteRule ^(?:article|relationships)/([0-9]*)\.html/?$ article/article.php?id=$1 [NC,L]
##Rules for 404 handling.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \.(js|css|jpeg|jpg|gif|png|ico|map)(\?|$) 404error.php [L,NC]
</IfModule>
I have two page and I'm trying to make good looking url for these links.
First link :-
http://www.themobilesapp.com/tags.php?tag=Android
for to convert this url into this url.
http://www.themobilesapp.com/tag/Android
for this i write this code.
RewriteRule ^tag/([A-Za-z0-9-]+)?$ tags.php?tag=$1 [L]
Second link :-
http://www.themobilesapp.com/phones.php?title=Apple
for to convert this url into this url.
http://www.themobilesapp.com/brand/Android
for this i write this code.
RewriteRule ^brand/([A-Za-z0-9-]+)?$ phones.php?title=$1 [L]
But on both pages one same error is coming.
This error is coming on my page
Internal Server Error
and my whole htaccess file code is
Options +FollowSymLinks -MultiViews -Indexes
Options -Multiviews
<IfModule mod_rewrite.c>
DirectoryIndex index.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
ErrorDocument 404 404error.php
RewriteRule ^([A-Za-z0-9-]+)?$ specification.php?url=$1 [L]
RewriteRule ^news/([A-Za-z0-9-]+)?$ news.php?url=$1 [L]
RewriteRule ^social/([A-Za-z0-9-]+)?$ social.php?url=$1 [L]
RewriteRule ^tag/([A-Za-z0-9-]+)?$ tags.php?tag=$1 [L]
RewriteRule ^brand/([A-Za-z0-9-]+)?$ phones.php?title=$1 [L]
</IfModule>
All RewriteRule rules are working except below two.
Please check for spellings and make sure file exist, try to use below simplified rule.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#after above rewrite cond use below rewrite for each page in below manner
RewriteRule ^tags/([\w-]+)$ tags.php?tag=$1 [L]
i'm working on a website at work and recenctly move it via svn to my localserver so i can work from home...
The setup went okay, no majors... but code ignighter sends 404 Not Found on pages along with the correct response...
For example i can load 'localhost/home' and it fires my controller and i get correct view, but CI also send 404 in headers for the page....!!
this also happens on my js files...
I can force it to send "200 OK", but doing this on every page seams silly..
Has anyone come accross this problem before...?
I'm using;
Code Ignighter: 2.1.0
Apache : 2.0.63
PHP: 5.1.6
this is my .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|robots\.txt|css|js)
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
Give the .htaccess example from the documentation a try:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
The following line could be responsible for your 404 response:
ErrorDocument 404 /index.php
My site is a php based site, but I've added wordpress in a /blog/ folder. The .htaccess file below should allow the /blog/ folder to be accessed, but I get a 404 error saying that blog.php doesn't exist.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !\.(gif|jpg|png)$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/(.*)/$ /$1_$2.php [L]
RewriteRule ^(.*)/$ /$1.php [L]
</IfModule>
Anybody able to help at all?
The last RewriteRule is redirecting your request to /blog/ to index.php, you should add a RewriteCond to check if the request is on the blog folder.
RewriteCond %{REQUEST_URI} !^/blog/.*
I managed this using the code below, for some reason the conditionals that were suggested don't work (I HATE .htaccess)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(blog) - [L]
RewriteCond %{SCRIPT_FILENAME} !\.(gif|jpg|png)$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/(.*)/$ /$1_$2.php [L]
RewriteRule ^(.*)/$ /$1.php [L]
</IfModule>
Adding
RewriteRule ^(blog) - [L]
to public_html/.htaccess after
RewriteEngine On
worked for me as well on a fresh Wordpress installation with Fantastico on a Hostgator account, with a blog.example.com subdomain.