URL rewrite htaccess - php

I have a small website where I have wrote some rules
RewriteEngine On
RewriteRule ^[!/.]*([A-Za-z0-9]+)/?$ /index.php?id=$1 [NC,L]
This redirects all of my links to example.com/index.php. Whereas I want them to redirect all the URLs with one directory example.com/dir to be redirected to this link example.com/index.php, and URLs with two directories, example.com/dir1/dir2 should redirect to example.com/index2.php, and rest should not be redirected.

you can use the below code .. i'll explain it a little ..
http://www.example.com/customdir/index/1/
customdir = Your Sub Directory
index = index.php
1 = Your id
Options +FollowSymLinks
RewriteEngine on
RewriteRule customdir/(\w+)/?(\d+)?/? customdir/$1.php?id=$2

Related

Rewrite Rule for subfolder instead of main website

I have a htaccess code that i want to be changed.
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)$ proxy.php?url=$1 [L,QSA]
It works like a charm if i place proxy.php and .htaccess file in main website and visit pages through www.domain.com/
I want to change this RewriteRule in a way that if I place the proxy.php and htaccess in subfolder named "folder" then new results show in www.domain.com/folder/
So mainly purpose is to change results path from www.domain.com to www.domain.com/folder/
You can place your code without any changes in /folder/.htaccess with proxy.php.
Or you can use:
RewriteEngine on
RewriteBase /
RewriteRule ^folder/(.+)$ folder/proxy.php?url=$1 [L,QSA]

Redirect subdirectory to root subdirectory without changing url PHP

https://example.com/app1
This is my subdirectory and it has one index.html file running on apache server.
I need to redirect all the requests to this directory like https://example.com/app1/* to https://example.com/app1 without changing the URL.
If user access to https://example.com/app1/test it should be redirected to https://example.com/app1 but URL should remain same as
https://example.com/app1/test
You can use this rule in your public_html/. htaccess :
RewriteEngine on
RewriteRule ^app1/((?!index\.html).+)$ /app1/ [NC,L]
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^([a-zA-Z0-9_-]*)$ index.php [nc,qsa]
This one worked for me.
Thanks to all.

Codeigniter htaccess redirections not redirecting to desired url

I have a codeigniter website which I want to do seo on. So i used routes for sluggish urls rather than the codeigniter default having method name and indices.
With this a problem comes in play, three different urls it opens the same page ie.
1. https://www.example.com/seo-freindly-url/
2. https://www.example.com/index.php?/controller/method/42
3. https://www.example.com/controller/method/42
But I want only the first url to work, for others i want to redirect to the seo friendly one by only using htaccess redirections.
Please Help me in that.
One more thing i used htaccess 301 redirection to redirect one of my urls
Redirect 301 "/plan/index/42" https://www.example.com/services/seo
It works to some extent, whenever I open the url
https://www.example.com/plan/index/42
it redirects to
https://www.example.com/services/seo?/plan/index/42
but it has to be https://www.example.com/services/seo/
Thank you.
It's hard to test on my end, but you might try something like this:
.htaccess
RewriteEngine On
RewriteBase /
RewriteRule ^controller/method/42$ /seo-friendly-url [L]
redirect 301 /plan/index/42 /services/seo
RewriteRule ^(system|application|cgi-bin) - [F,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Remove index.php from all URLs
RewriteRule .* index.php/$0 [PT,L]
/application/config/config
// This assumes you want to remove index.php from all URLs
$config['index_page'] = '';

Redirect 301 Apache to new site - Folders

I wan't to redirect
www.site123.com/images/folder01/img01.jpg
to
www.site456.com/images/folder02/img01.jpg
I want to redirect the folder to folder separately. One line for each folder.
Entering in:
www.site123.com/images/folder01/sub01/sub02/999.xyz
Should be redirected (301) to:
www.site456.com/images/folder01/sub01/sub02/999.xyz
Since the directory structure from site123 to site456 look identical, have you tried the following global redirect?
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.site456.com/$1 [R=301,L]
If directory/specific then change your rule to something like this:
RewriteRule ^/folder/(.*) http://www.site456.com/folder/$1 [R=301,L]
If you want sub-folders too:
RewriteRule ^Images/?(.*) http://www.site456.com/Images/$1 [R=301,L]
Create a line for each pattern you wish to match and change the corresponding redirect URL/folder path. With rewrites it's sometimes trial/error too so take this and try it, then tweak as needed.

htaccess redirection rules with conditions

I used the following code in my htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
</IfModule>
It redirects as if the url is test.com/test --> it redirects to test folder (Here the folder is available in the name of 'test')
If the url is test.com/testpage --> it redircts to index.php (i.e no one folder is in the name of testpage)
But i want to redirect this as
if the url is test.com --> it will redircts to index.php
if the url is test.com/test --> It will redirects to corresponding folder (if the folder name exists)
if the url is test.com/testpage then it redirects to page.php
Also i want to redirect some particular name to test.php (test.com/test2 or test.com/test3)
Please help this
This is done by creating a router class.
The code you have there redirects all urls to index.php making index be able to read the url and base it's contect on that.
see http://www.phpaddiction.com/tags/axial/url-routing-with-php-part-one/
for router tutorial
you can individually write their rewrite conditions for specific pages/urls or just add some sort of suffix like along with page pass "[category]/testpage" or any thing and describe the rewrite condition so that it can be redirected.
RewriteRule ^category/(.*)$ page.php [R=301]

Categories