I am currently trying to write a rewrite rule. I want to simplify the entered URL so that it will always use the index. The input url would be www.example.com/home.php and would be rewritten to www.example.com/index.php?r=page/home.
I wrote this .htaccess file:
RewriteEngine On
RewriteRule ^([^/]*)\.php$ /index.php?r=page/$1 [L]
This configuration unfortunately gives me an error 500. I can understand that it is caused by the fact that if I enter index.php for instance, apache will not know if it must use the index.php file or use the rewritten url index.php?r=page/index.
Is it possible to accomplish this kind of rewriting rule with apache? If so, how can I fix my error 500?
Edit: Please note that the RewriteRule works fine if I change the extension .php to anything else such as .html, as so: RewriteRule ^([^/]*)\.html$ /index.php?r=page/$1 [L]
You have two possibilities.
First one
RewriteEngine On
RewriteRule ^index\.php$ - [L]
RewriteRule ^([^/]+)\.php$ /index.php?r=page/$1 [L]
Second one
RewriteEngine On
RewriteCond %{THE_REQUEST} \ /([^/]+)\.php
RewriteRule ^.*$ /index.php?r=page/$1 [L]
you can also try with this :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?r=page/$1 [L]
Related
Actually I need to add index.php in my application URL through htaccess file.
My URL is like this.
http://localhost:8080/myapp/xyz/abs.html
I need to change this into.
http://localhost:8080/myapp/index.php/xyz/abs.html
Can anyone tell me what i need to be write in htaccess file.
Any Help will be appreciating.
Thanks.
Have this rule in /myapp/.htaccess:
RewriteEngine On
RewriteBase /myapp/
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.+) index.php/$1 [L]
Presumably an internal rewrite is required, not an external redirect? In which case, try something like the following, using mod_rewrite in your root .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_URI} !/index\.php/
RewriteRule ^myapp/(.*) /myapp/index.php/$1 [L]
The RewriteCond directive is required to prevent a rewrite loop. (Only rewrite if it doesn't already contain "/index.php/".)
Try this in your htaccess
RewriteEngine on
RewriteCond %{THE_REQUEST} /myapp/xyz/([^.]+)\.html [NC]
RewriteRule ^ /myapp/index.php/xyz/%1.html [R,L,NC]
I am trying to rewrite the url using htaccess and I have tried answers given on another questions here however nothing seems to be working at all I still get the original url.
this is what I have:
http://localhost/inbox.php?pg=2
I want
http://localhost/inbox/2
I already had a rule that gets rid of the .php extension in .htaccess as below and just added the last line
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [QSA,L]
RewriteRule /(.*)$ /inbox.php?pg=$1//added this
Your problem is that the line before the last one is defined as the last one so your rule must be above that RewriteConditions. Better use this rule set:
RewriteRule ^/?inbox/(\d+)$ /inbox.php?pg=$1 [QSD,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [QSA,L]
I added your needed prefix which you missed and made it mandetory that after that numbers will follow (at least one).
Apache rewrite engine is mainly used to turn dynamic url’s such as http://localhost/inbox.php?pg=2 into static and user friendly url’s http://localhost/inbox/2
RewriteEngine on
RewriteRule ^inbox/([^/.]+)/?$ /inbox.php?pg=$1 [L]
Explanation: How this work
Call to action: RewriteRule
Pattern: ^inbox/([^/.]+)/?$
Rewrite: /inbox.php?pg=$1
Command Flag: [L]
Simply,
RewriteEngine on
RewriteRule ^inbox/([0-9]+)/?$ inbox.php?pg=$1
Source: 10 Simple examples to rewrite using htaccess
I am having some issues with url rewriting
I am using this as my code for rewrite in htaccess
RewriteEngine On
RewriteRule ^([^/]*)\.html$ /pages.php?id=$1 [L]
this was my url
studentsassignmenthelp.com/pages.php?id=Marketing-Assignment-Help
after using the url rewrite i am getting this
studentsassignmenthelp.com/Marketing-Assignment-Help.html
but i need it without the html like the below one
studentsassignmenthelp.com/Marketing-Assignment-Help
if i use the below code to remove the .html than it shows error 500 server...
RewriteRule ^([^/]*)$ /pages.php?id=$1 [L]
I saw some example but after using them it shows 404 or 500 error
Try this,
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !pages.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ pages.php?id=$1 [QSA,L]
try this:
RewriteEngine On
RewriteRule ^([^/]*)$ /pages.php?id=$1 [L]
redirectMatch 301 ^(.*).html $1
This is quite usefull to remove any url extension and avoid broken links.
I have a setup that sets variables for the index page, but it also does the same for directories and I don't want it to do that. Here's my .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/(login|images|favicon\.ico|home|about|sitemap|contactus|termsandconditions|privacypolicy|signup|search|careers|error|css|js) [NC]
RewriteRule ^([a-zA-Z0-9]+)$ index.php?name=$1
RewriteRule ^([a-zA-Z]+)/$ index.php?name=$1
RewriteRule ^([a-zA-Z]+)/([a-zA-Z0-9_]+)$ index.php?name=$1&page=$2
RewriteRule ^([a-zA-Z]+)/([a-zA-Z0-9_]+)/$ index.php?name=$1&page=$2
RewriteRule ^php/$ error/
Now, with this setup, if I type in mysite.com/login it will redirect to the index.php page and set login as the name variable. How do I make it to where it ignores the directories? This is frustrating me and I've looked through this site for over an hour for an answer and can't find a similar question (I might suck at that too, though. haha!).
Also, if you look at the last RewriteRule, you can see that I'm trying to redirect any attempt to access my php/ folder to my error/ folder. This is also not working.
RewriteCond only applies to the immediately following RewriteRule. Also, you can combine lines 3&4, and 5&6 respectively, by using /? on the end, which makes the / optional (regex).
Your file could be similar to this:
RewriteEngine On
#the following line will not rewrite to anything because of "-", & stop rewiting with [L]
RewriteRule ^(login|images|favicon\.ico|home|about|sitemap|contactus|termsandconditions|privacypolicy|signup|search|careers|error|css|js)/?(.*)$ - [L,NC]
RewriteRule ^php/?(.*)$ error/ [L,NC]
RewriteRule ^([^/]+)/?$ index.php?name=$1 [L]
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?name=$1&page=$2 [L]
You may be interested in the Apache Mod_Rewrite Documentation.
RewriteCond %{REQUEST_URI} !^/(login|images|favicon\.ico|home|about|sitemap|contactus|termsandconditions|privacypolicy|signup|search|careers|error|css|js) [NC] !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
to either not execute the rule on a directory or a file
More info on the mod_rewrite documentation pages, search for CondPattern
I am quite new to PHP and just getting started with mod_rewrite. I know the basic lingo but come stuck when I want to simply refer to the route directory
i.e. this is not probs
RewriteRule ^settings/$ settings.php
[QSA,L]
But how to for example make:
RewriteRule ^page/(.*)$
index.php?Page=$1 [QSA,L]
which generates /page/[page-name]
Just become
/[page-name]
?
Maybe I didn't understand you but it seems that you need such .htaccess file to solve your problem.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Ignore valid files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?Page=$1 [QSA,L]
</IfModule>
This should do it:
RewriteRule ^(.*/)$ index.php?Page=$1 [QSA,L]
However, you should place that rewrite rule after all the other specific rewrite rules you have, otherwise all requests will be redirected to index.php?Page=....