is if else conditions available on htaccess? - .htaccess

I have a .htaccess file in the folder "services" in my website
and have to give two urls following
content of htaccess
#------------------------
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^(.*)$ index.php?n=$1 [L,QSA]
</IfModule>
$-------------------------
1. http://www.mysite.com/services/seo-work
2. http://www.mysite.com/services/online-editor
The 1st URL rewritten using htaccess like http://www.mysite.com/services/index.php?s=seo-work
This is working correct. but when i am uploading other file online-editor.php on the same folder "services", it is not working
so how the first url should show the page using index.php?s=seo-work if the page "online-editor.php" is not found in directory otherwise online-editor.php shoud show. I am programatically representing my problem below
if(IsFoundPageInDirectory(online-editor.php))
{
GoToUrl(http://www.mysite.com/services/online-editor.php)
}
else
{
GoToUrl(http://www.mysite.com/services/index.php?s=seo-work)
}
Please help me to get out of this problem
Advance thanks for your reply

There is not really if/else conditions with ifModule in Apache configuration, but you can use the test in 2 cases : !module & module, like this:
<IfModule !module>
# default directives
</IfModule>
<IfModule module>
# decorator directives
</IfModule>

Check default wordpress .htaccess file. I think you're missing checks for existing files and directories. Something like
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>

Last I found answer.. thanks for the fist reply to lead me to get answer
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?n=$1 [L,QSA]
</IfModule>

Related

.htaccess - Read all sub-directories and files by one file

I am trying to add a .htaccess file into domain.com/links/ so if anyone goes to domain.com/links/mylink/ or domain.com/links/mylink it is passed to domain.com/links/link.php?link=xxxxx but I just can't seem to find the RewriteRule that doesn't keep repeating itself.
RewriteRule ^(.*)$ /links/link.php?link=$1 [L]
The above does the following
domain.com/links/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/something
<IfModule mod_rewrite.c>
RewriteEngine On
# If file exists, don't rewrite
RewriteCond %{REQUEST_FILENAME} !-f
# Rewrite everything to link.php
RewriteRule . /links/link.php [L]
</IfModule>

.htaccess change parameters received directly after domain

I have been looking for many tutorials bu no one has really helped me, I know you guys can:
My website has the following structure:
mywebsite.domain/?pg=somepage
But I would like to leave it like this:
mywebsite.domain/somepage
All the tutorials I have found work in the next structure:
mywebsite.domain/file.php?pg=somepage
So I can't gess how a RewriteRule would be using my structure.
I have tried:
RewriteEngine on
RewriteRule ^index/([0-9a-zA-Z]+) index.php/?pg=$1
And also:
RewriteEngine on
RewriteRule ^index/([0-9a-zA-Z]+) index.php?pg=$1
But I have had no luck :(
Note: My index file has .php extension
You could do something like this -
RewriteRule ^(.*)$ /index.php?pg=$1 [L]
This will redirect mywebsite.domain/somepage to mywebsite.domain/index.php?pg=somepage
or full config -
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?pg=$1 [L]
</IfModule>
for more info - Apache mod_rewrite

.htaccess redirect not working with index.php of sub directory

I have a directory structure like root/user/confirm/index.php
I want to make redirect like
Redirect this url
http://www.site.com/user/confirm/ASd2sasda4ass
To this
http://www.site.com/user/confirm/index.php?confirm=ASd2sasda4ass
I am trying this way
RewriteRule /user/confirm/([^A-Za-z0-9])$ /user/confirm/index.php?confirm=$1 [L,QSA]
It redirects this url properly
http://www.site.com/user/confirm/index.php/ASd2sasda4ass
To
http://www.site.com/user/confirm/index.php?confirm=ASd2sasda4ass
But not working for this url without index.php
http://www.site.com/user/confirm/ASd2sasda4ass
It shows 404 not found error
Please see and suggest any possible way to do this.
Thanks.
UPDATE
Complete codes
Options +FollowSymlinks
RewriteEngine on
<IfModule mod_rewrite.c>
RewriteRule /user/confirm/([^A-Za-z0-9])$ /user/confirm/index.php?confirm=$1 [L,QSA]
</IfModule>
Try this (without confirm.php in the url. You shouldn't need it anyway):
RewriteRule ^user/confirm/(.*)/? user_confirm.php?confirm=$1 [L]
I solved this issue by removing this rewrite rule from root .htaccess
RewriteRule ^/user/confirm/([^A-Za-z0-9])$ /user/confirm/index.php?confirm=$1 [L,QSA]
And placing another .htaccess file in confirm directory to hide index.php and processing query parameter there with following codes.
Options +FollowSymlinks
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?confirm=$1 [L]
</IfModule>
And now its working without index.php in url. Hope this helps others too with same issue.

How to map URL with .htaccess

My local site have project folder http://127.0.0.1/myproject/index.php. I want to be able to map its URL so if someone enters http://127.0.0.1/myproject/service, it should display the same content or perform the same functionality as index.php does.
I tried some example on different site but couldn't get it done.
What I tried so far is :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)\myproject\service$ $index.php
RewriteEngine On
RewriteBase /myproject/
RewriteRule service index.php [L,QSA]
First, setup the base to rewrite. In the rule, all paths are now relative to the base, not absolute.
The flag L denotes no other rules should be applied.
The flag QSA denotes that the query string should be appended.
For more information: http://httpd.apache.org/docs/current/mod/mod_rewrite.html
I would even go as far as to say, check what Wordpress is giving you:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /myproject/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /myproject/index.php [L]
</IfModule>
# END WordPress

.htaccess adding new file

<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /homepage/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /homepage/index.php [L]
</IfModule>
That is my .htaccess code. I have created new file in homepage/test.php and when i visit it from browser it give error of access,
i have tried by replacing index.php with test.php but it still not working.
Can some one explain me how this code work and why not my code is working.
i have deleted .htaccess but its still not working. What else file it can be.
Change
RewriteRule . /homepage/index.php [L]
to:
RewriteRule (.+) /homepage/index.php?page=$1 [L]
P.S. For this to work, your index.php needs to listen for $_GET['page'] and load the appropriate resource.
EDIT: without using the "?page=$1" your code will just display the index page for all requests that are not files or directories. I am not sure if that is what your were going for.

Resources