Using mod rewrite to rewrite a particular url string in codeigniter - apache

I'm not sure if mod rewrite should be used here:
I'd like to rewrite one url:
www.baseurl.com/url-with-hyphens
to:
www.baseurl.com/controller/method
This is a codeigniter application, here are my current rules:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
I have tried:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
RewriteRule ^.registration/available/2 /u-s-course-schedules/
Thanks

Related

Combine htaccess rules

I have the following .htaccess in parent directory :
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} login.php [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
Then i have another .htaccess in a sub directory :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*) /mobile/new_mobile/index.html [NC,L]
Each of them separately does its job and works fine, but i want to use them both combined but cant seem to figure out how, i tried both :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*) /mobile/new_mobile/index.html [NC,L]
RewriteBase /
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} login.php [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
and
RewriteEngine on
RewriteBase /
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} login.php [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*) /mobile/new_mobile/index.html [NC,L]
You can use this combined .htaccess in site root .htaccess (parent directory):
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} /login\.php [NC]
RewriteRule ^ https://%{HTTP_HOST}/%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^sub/(.*) /mobile/new_mobile/index.html [NC,L]

How to create multiple rewrite rules in .htaccess

I want to have this kind of structure in .htaccess. Is it possible?
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule ^signup$ /signup.html [L]
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule ^signin$ /signin.html [L]
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule ^home$ /landing.html [L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^ index.html [L]
</IfModule>
For landing, signin, signup I want to request separate pages and for all other requests I need to request index.html page. Can I do that with .htaccess?
Have it like this:
DirectoryIndex index.html
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} /index\.html$ [NC,OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^(signup|signin)/?$ $1.html [L,NC]
RewriteRule ^home/?$ landing.html [L,NC]
RewriteRule . index.html [L]

.htaccess multiple rewrite rules on multiple php files not working

I'm new on .htaccess and rewrite rules. So if my question is not relevant, please forgive me.
I have below Rewrite rule;
RewriteRule ^\yazarlar$ /writer-list.php [L]
I'm trying to achieve if someone hit url http://example.com/yazarlar or http://example.com/yazarlar/ they should see http://example.com/writer-list.php file. But it's not working.
Am i missing something?
FULL .htaccess
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond ${REQUEST_URI} ^.+$
RewriteCond %{REQUEST_FILENAME} \.(gif|jpe?g|png|js|css|swf|php|ico|txt|pdf|xml)$ [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]
## If the request is for a valid directory
RewriteCond %{REQUEST_FILENAME} -d [OR]
## If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f [OR]
## If the request is for a valid link
RewriteCond %{REQUEST_FILENAME} -l
## don't do anything
RewriteRule ^ - [L]
RewriteRule ^haber/([^/]+)-([^/]+)/?$ article.php?link=$1&i=$2 [L]
RewriteRule ^([^/]+)/([0-9]?)?$ /article-list.php?link=$1&page=$2 [L]
RewriteRule ^([^/]+)?$ /article-list.php?link=$1 [L]
RewriteRule ^\yazarlar$ /writer-list.php [L]
No need to escape the \y and remove redundant rules:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} \.(gif|jpe?g|png|js|css|swf|php|ico|txt|pdf|xml)$ [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]
RewriteRule ^yazarlar/?$ writer-list.php [L,NC]
RewriteRule ^haber/([^/]+)-([^/]+)/?$ article.php?link=$1&i=$2 [L,QSA,NC]
RewriteRule ^([^/]+)/([0-9]+)/?$ article-list.php?link=$1&page=$2 [L,QSA]
RewriteRule ^([^/]+)/?$ article-list.php?link=$1 [L,QSA]

Error The requested URL /public/ was not found on this server when run sub folder in zend 2

I have a project in www/zendtest
This is my folders structure:
Contents of www/zendtest/.htaccess:
RewriteEngine On
RewriteRule ^\.htaccess$ - [F]
RewriteCond %{REQUEST_URI} =""
RewriteRule ^.*$ /public/index.php [NC,L]
RewriteCond %{REQUEST_URI} !^/public/.*$
RewriteRule ^(.*)$ /public/$1
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^.*$ - [NC,L]
RewriteRule ^public/.*$ /public/index.php [NC,L]
Contents of www/zendtest/public/.htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteRule ^(.*)$ %{ENV:BASE}index.php [NC,L]
When I run http://localhost/zendtest the result is the following error:
404 Page Not Found
The requested URL /public/ was not found on this server.
How do I avoid having the /public/ folder name so that I can run this on the domain localhost/zendtest?
The problem arises because of the following:
RewriteRule ^.*$ /public/index.php [NC,L]
You have the directory www to be your DocumentRoot. When you rewrite the URL's to /public/index.php, Apache is trying to search for www/public/index.php file, which of course, does not exist.
Use a RewriteBase directive:
RewriteEngine On
RewriteBase /zendtest/
RewriteRule ^\.htaccess$ - [F]
RewriteCond %{REQUEST_URI} ^/zendtest/?$
RewriteRule ^.*$ public/index.php [NC,L]
RewriteCond %{REQUEST_URI} !^/zendtest/public/.*$
RewriteRule ^(.*)$ public/$1
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [NC,L]
RewriteRule ^public/.*$ public/index.php [NC,L]

RewriteRule for htaccess to hide default language

In Zend configuration defaults I have these rules:
SetEnv APPLICATION_ENV offline
RewriteEngine On
Options +FollowSymlinks
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
When i try to add fallowing line, notfing happens:
RewriteRule ^en/(.*) $1 [L]
On result I expact to rewrite permomently http://example.com/en/something to http://example.com/something
At now I have both links worked separatly.
Edited:
SetEnv APPLICATION_ENV offline
RewriteEngine On
Options +FollowSymlinks
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteCond %{REQUEST_URI} !^/en/
RewriteRule ^.*$ index.php [NC,L]
RewriteRule ^en/(.*) /$1 [L,R]
This will redirects default language urls directly to site root!
Many thanks.
There is for LigHTTPD:
$HTTP["url"] != "^/en/" {
url.rewrite-once = (
"^/.*" => "index.php?/$1",
)
}
url.redirect = (
"^/en/.*" => "/$1",
)
It's bceause RewriteRule ^.*$ index.php [NC,L] is rewriting it and it never gets to the rule you added. You need to add a condition so that requests starting with /en/ don't get rewritten to index.php:
# you regular stuff
RewriteEngine On
Options +FollowSymlinks
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
# Add this condition
RewriteCond %{REQUEST_URI} !^/en/
RewriteRule ^.*$ index.php [NC,L]
# Now you can rewrite /en
RewriteRule ^en/(.*) $1 [L,R]
Edited: example.com/en/something gets redirected to example.com/something, then rewritten to index.php.