Rewrite URL in php using htaccess file with root directory - .htaccess

Show URL like this:
localhost/test/view/admin/process.php?action=dashboard
I want show my URL like this below:
localhost/test/general/dashboard/
Test is root directory,
general is like module name and
dashboard is page name

Try this:
RewriteEngine on
RewriteRule ^/general/([a-zA-Z0-9_-]+)$ /view/admin/process.php?action=$1 [L]
Your question has been answered countless times, googled before ask.

RewriteRule localhost/test/general/(.*)/ localhost/test/view/admin/process.php?action=$1 [L]
bad url format

Related

.htacces redirect specific folder/subfolder

I am trying to make this work, please help me in it, if you can:
1.
I have a specific folder on an old domain, with a lot of subfolder.
Looking like this:
OLD-DOMAIN / FOLDER / something1 etc.
2.
I would like to redirect it to a new domain's specific ABCD folder.
NEW-DOMAIN / ABCD / FOLDER / something1 etc.
I have been using this code:
RewriteCond %{REQUEST_URI} ^/(FOLDER)$
RewriteRule ^ http://www.newdomain.com/**ABCD**/%1 [L,R=301,NC]
The code is working just fine for the first FOLDER, but not for the deeper subfolders. The /something1 doesnt redirecting through the chain.
How can I make this special redirect to every subfolders?
Thanks in advance!
Just change your rule to this:
RewriteRule ^FOLDER(/.*)?$ http://www.newdomain.com/ABCD/$0 [L,R=301,NC,NE]
Clear your browser cache before testing this change.

Show index page for all links to folder

I have a folder named "keywords" for my website, such as this: http://www.mysite.com/keywords/
In the folder I only have an index.php file. Regardless of what someone types in after the folder, I need the index.php to show. For example, http://www.mysite.com/keywords/this-is-a-test should show the index.php file's content but without changing the URL.
Everything I've tried seems to fail - I'm working with the .htaccess in my /keywords/ folder. Regardless of what I put there I seem to keep getting a 404 error. Any help would be appreciated, I'm sure it's something small and simple that I just don't know how to do.
This is what I currently have:
RewriteEngine on
RewriteRule - index.php [L]
The problem with this is that it only work when going to links with a dash in them.
For example,
http://www.mysite.com/keywords/testing-this
does work. But this does not work:
http://www.mysite.com/keywords/testingthis
Any help would be appreciated.
RewriteEngine On
RewriteBase / #make sure your website is located in root directory, if not you have to add directory here...
RewriteRule ^keywords/testingthis$ /keywords/index.php$1 [L]

Url rewriting with parameters

I’m a noob in url rewriting and i really need help to figure this out :
I have this url : section/news.php?url=section/news/27-how_to_make_rewrite.html I want to access this news with the url parameter and the link would be : section/news/27-how_to_make_rewrite.html
the .htacess file is in the root, i have a folder named section and inside this folder i have the news folder
This doest work for me.
I have :
RewriteEngine on
RewriteBase /section/
RewriteRule news/^([a-zA-Z0-9-+/]+)/$ news/news.php?url=$1
How I can do it ?
I have not used this with subfolders, but I would try something like this.
RewriteRule ^news/([^/\.]+)/?$ news/news.php?url=section/news/$1 [L]
Edit
Try it this way:
RewriteRule ^section/news/([^/.]+)/?$ news.php?url=section/news/$1 [L]
You normally only want to put what is changing in the variable. section/news/ is always used so you should keep it outside of the replacement.
If the URI you want to rewrite to looks like: section/news.php?url=section/news/27-how_to_make_rewrite.html
then you've got one too many "news" in your target. You also need to fix your regex pattern to match stuff like 27-how_to_make_rewrite.html.
You want something like:
RewriteEngine on
RewriteBase /section/
RewriteRule news/(.+)/?$ news/news.php?url=section/news/$1

CodeIgniter Path Issue

I am use Codeigniter to creat my site. it is a huge site with a lot of contents.
i use the default welcome controller for all pages.
the path is like this now
http://mydomain.com/index.php/welcome
and my folder structure is like this
/root
/codeigniter Application folder
/controller
/view
/static/
/images/
/css/
/pdf/
.htaccess
because i am doing the content first, so all the images are set to absolute patch (http://mydomain.com/static/images/foldername/pc.jpg)
now when i use the rewrite to remove "inde.php" and "welcome"
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt|system|pdf|sitemap\.xml|profile.htm|^([A-z,0-9,_,-]+).asp|(.*)\.pdf|phpadmin)
RewriteRule ^(.*)$ /index.php/welcome/$1 [L]
all css and image file are not accessible anymore. I have nearly a thousand pages with images. I don't have the time to change the page one by one. the deadline is coming, please help.
thanks
Have you tried adding the css-folder to your excluded rewriteconditions?
RewriteCond $1 !^(index\.php|images|robots\.txt|system|pdf|sitemap\.xml|profile.htm|^([A-z,0-9,_,-]+).asp|(.*)\.pdf|phpadmin)
Does not seem to exclude the folder css from rewriting (see images is excluded).
Try something like
RewriteCond $1 !^(index\.php|images|css|robots\.txt|system|pdf|sitemap\.xml|profile.htm|^([A-z,0-9,_,-]+).asp|(.*)\.pdf|phpadmin)
Good luck
I think the problem lies with your .htaccess file. We have a similar setup, and that url living outside of the application directory is available just as you have it with a direct url relative to its path. Check out the wiki at http://codeigniter.com/wiki/mod_rewrite for the correct way to set up your .htaccess file.
Edit to clarify: The url http://mydomain.com/static/images/foldername/pc.jpg should work. something about your .htaccess file is wrong.
RewriteCond $1 !^(static|index.php|images|robots.txt|system|pdf|sitemap.xml|profile.htm|^([A-z,0-9,_,-]+).asp|(.*).pdf|phpadmin)

Use htaccess to mask folder name

Here's a problem I'm always wanting to solve with htaccess. I haven't found a way yet, though it looks like it should be possible - perhaps someone can help.
Let's say I have a folder at the root of my site called /foo/. I want users to be able to access that folder at the path /bar/, but for various reasons I can't rename the folder.
So as not to create confusion I only want one path to ever be seen - that is to say, I don't want people to use the name /foo/ to access the folder; they should always use /bar/. If someone goes to example.com/foo/, their browser should redirect to example.com/bar/ - but the content returned should be the content of /foo/.
To make matters more complicated, pages in /foo/ have dependencies (images, stylesheets, links to other pages, etc) within /foo/ which are hardcoded and can't be changed. These must, of course, still work.
So, to summarise, this is what I want :
Requests for example.com/foo/ should redirect to example.com/bar/.
Requests for example.com/bar/ should return the contents of example.com/foo/.
Is this possible? It looks on the surface as if it would create an infinite redirect... but I'm pretty sure there are ways to prevent that in htaccess, aren't there?
I'd be very grateful for any help.
(PS - for a little extra background: The normal reason I want to do this is to rename the wordpress /wp-admin/ directory to something more professional and easy for customers to remember, such as /admin/. But the same system should work for masking any path in this way.)
I found a sort of workaround - by using a symlink and htaccess in combination.
First I created a symlink from /bar to /foo/.
Then I put this in htaccess :
Options +FollowSymLinks
RewriteRule ^foo/(.*)$ bar/$1 [R,L]
This has exactly the desired result - example.com/bar/ shows the content of the /foo/ directory, and example.com/foo/ redirects to example.com/bar/
But if anyone can come up with a pure htaccess solution I'd much prefer that!
Update :
Ok, I've finally found out how to do this. It turns out to be quite simple...
RewriteCond %{THE_REQUEST} ^GET\ /foo/
RewriteRule ^foo/(.*)$ bar/$1 [R,L]
RewriteRule ^bar/(.*)$ foo/$1
The only problem is that it doesn't take account of RewriteBase, so you have to include the full path in the first line (after ^GET\).
If I understand correctly what you want is something like this inside your .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^foo/$ bar/
RewriteRule ^bar/$ foo/
</IfModule>

Resources