How to change URL structure with htaccess? - .htaccess

I have downloaded and installed http://www.question2answer.org/ on my website on http://www.yourstartups.com/discussion/ (discussion sub-directory)
Current url structure is like: example.com/index.php?qa=123&qa_1=why-do-birds-sing
I want to change to /123/why-do-birds-sing
(why-do-birds-sing is example query)
Following is my htaccess:
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteBase /
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php?qa-rewrite=$0&%{QUERY_STRING} [L]
</IfModule>
Can anyone help me?

You need a new rule to rewrite your pretty URL.
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteBase /
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?qa=$1&qa_1=$2 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php?qa-rewrite=$0 [L,QSA]
</IfModule>

Related

Url Rewrite - Add Query String Too 999

DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteBase /
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php?qa-rewrite=$0&%{QUERY_STRING} [L]
RewriteRule ^tags tags.php [L,NC]
</IfModule>
i want to add new page .but write page not found. why? please help me.
RewriteRule ^tags tags.php [L,NC] // i want this www.example.com/tags open tags.php ?
You need to reorder your htaccess rules like this :
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
RewriteRule ^tags/?$ tags.php [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php?qa-rewrite=$0&%{QUERY_STRING} [L]
</IfModule>
The problem is that, your rule
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php?qa-rewrite=$0&%{QUERY_STRING} [L]
Rewrites any request (not a true file/dir) to index.php. since /tags is not an existent file on your directory, so a request for http://example.com/tags was being rewritten to /index.php and the last rule(s) never executed.

Codeigniter: specified word after url redirect to subfolfer

I have Codeigniter 3 project in root directory. I need to use it with kohana other project which is in subfolder (admin).
I need to make redirect, when I will type mysite.xyz/admin that will redirect me to subfolder admin, where are kohana files: index.php etc.
Now CodeIgniter think that admin is a controller.
My .htacces file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /projekty/folder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteRule admin/^(.*)$ /admin/ [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
Have any ideas, how to solve that? I was trying to find some solutions, but no success.
Here is Kohana .htaccess:
RewriteEngine On
RewriteBase /projekty/folder/admin/
###### Add trailing slash (optional) ######
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [L,R=301,NE]
RewriteCond %{REQUEST_METHOD} !POST
RewriteRule ^(.*)home/u343855449/public_html/projekty/folder/admin/index.php/(.*)$ /$1$2 [R=301,L,NE]
RewriteCond $1 ^(index\.php|robots\.txt|favicon\.ico|media)
RewriteRule ^(?:application|modules|system)\b.* index.php/$0 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?kohana_uri=$1 [L,QSA]
your admin rule is in the wrong place. It needs to come before the CI rules. Put it below the rewritebase. Try these rules.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /projekty/folder/
RewriteRule ^admin/(.*)$ /admin/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/admin [NC]
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
Actually you really don't need the admin rule. Just tell it to ignore admin in the CI rules as below.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /projekty/folder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/admin [NC]
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

Htaccess conflict rules

My Htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^home/?$ index.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?([^/]+)/?$ departure.php?PDt2=$1 [QSA,NC,L]
RewriteRule ^(.+)/(.+)/(.+)$ service.php?DPt=$1&CTg=$2&NLs=$3 [QSA,NC,L]
</IfModule>
The rule of service.php url does not work.
Why the error occur?
Try changing your rules around and adding conditions to both rules. See if this helps.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^home/?$ index.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ service.php?DPt=$1&CTg=$2&NLs=$3 [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?([^/]+)/?$ departure.php?PDt2=$1 [QSA,NC,L]
</IfModule>

RewriteCond to catch URLs not ending with /

I'm trying to write a RewriteCond to my htaccess file to catch only URL's that DO NOT end with a / .
This is my file so far:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ ./wedding.php?w=$1
So I'm trying to pass the "W" GET var only if the URL does NOT end with /
So this will not be rewritten:
domain.com/bla/
But this will:
domain.com/bla
Please assist
Cheers!
Answer:
This is what I ended up using:
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ ./wedding.php?w=$1
Edit For Howlin:
This is my full htaccess, I have wordpress and a web app living together on the same dir, so if the URL isnt for the webapp it will go to Wordpress:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ ./wedding.php?w=$1
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
This should work for you:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([A-Za-z0-9]+)$ /wedding.php?w=$1 [R]
The above should only redirect urls that don't exist and don't have a / at the end.

Rewrite rule to end all URL's with .html including /index.html in the root and other directories

My question is, How can I rewrite all of my URL's in a way that will add .html to the end of every URL?
here is my .htaccess rule that I have
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1
</IfModule>
I have tried adding .html to index.php/$1.html but this is throwing 404 all over the place.
Please help
You can use:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^.]+?)/?$ $1.html [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^.]+)\.html$ index.php/$1 [L,NC]
</IfModule>

Resources