Url rewriting in folder project - .htaccess

I use subdomains a lot for webs and everytime i use url rewriting like this one:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/?$ index.php?key=$1
so if i use sudomain.domain.com/something its ok and it shows me index.
But now i try to use it on website which i have only in folder so adress is like.
domain.com/newwebsite/
i need same system here, so if i put domain.com/newwebsite/somepage it will be ok and it shows me index page like example before.

Try this rule in /newwebsite/.htaccess:
RewriteEngine On
RewriteBase /newwebsite/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ index.php?key=$1 [L,QSA]

Related

redirecting with htaccess causes too many redirects

The website I'm working on is using some cms. I need to add a static website to this. When I put mypage.html in the main directory and go to www.website.com/mypage.html it works. I would like the page to be accessible without '.html' ending. I experimented with editing htaccess files but always end up with error of too many redirections.
What I entered were various combinations, for example
Redirect 301 http://website.com/mypage http://website.com/mypage.html
The htaccess file I'm using looks like this:
:Location /*.php
Use php54
:Location
RewriteEngine On
DirectoryIndex index_prod.php
Options -Indexes
RewriteRule ^.*\.(css|png|swf|js|gif|jpeg|jpg|flv|pdf|doc)$ - [L]
RewriteRule ^net2ftp - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^/?$ plug.html [L]
#RewriteCond %{REQUEST_URI} !=/
RewriteRule ^/?.* index_prod.php
I'm looking for tips or to be explicitly told what and where to put in htaccess file to make it work (if it's possible)
Could you please try following, considering that you want without extension file URLs to be served by html extension files. Also since you didn't mention any specific condition before RewriteRule hence that redirection errors are coming to it, because its keep on redirecting in lack of any condition/check's presence(till its maximum redirection limit is crossed).
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ /$1.html [NC,L]

Basic URL rewriting: How to have a css link, img src, etc, ignore it?

I have a website running at localhost/pm and the RewriteBase is correctly set to /pm/. There is a link tag in my document: <link ... href="themes/default/css/default.css">.
When the url is localhost/pm or localhost/pm/foo the CSS works all right. When there are more slashes in the URL, however, like localhost/pm/foo/bar the relative URL if the stylesheet changes to foo/themes/default/css/default.css.
How do I get this to work without having to put some sort of PHP path resolution in the link tag?
# invoke rewrite engine
RewriteEngine On
RewriteBase /pm/
# Protect application and system files from being viewed
RewriteRule ^(?:system)\b.* index.php/$0 [L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php/$0 [PT]
EDIT:
Basically what I need now is this:
If request contains folder name /themes/ scrap everything that is before /themes/ and rewrite the rest to /pm/themes/...
I tried it like this: RewriteRule ^.*(/themes/.*)$ /pm/themes/$1 but I get an internal server error. Why?
If I do it like this: RewriteRule ^.*(/themes/.*)$ /pm/themes/ (ie. just remove $1 from the end) and use the URL http://localhost/pm/foo/themes/foo/ the resulting physical location is http://localhost/pm/themes which is what is expected too, which in turn means that at least my regex is correct. What am I missing?
The RewriteRule is almost correct
RewriteRule ^.*(/themes/.*)$ /pm/themes/$1
This rewrites http://localhost/pm/foo/themes/default/css/default.css to http://localhost/pm/themes/themes/default/css/default.css, which is one themes too much. Use this instead
RewriteRule /themes/(.*)$ /pm/themes/$1 [L]
But now you have an endless rewrite loop, because /pm/themes/.. is rewritten again and again. To prevent this, you need a RewriteCond excluding /pm/themes
RewriteCond %{REQUEST_URI} !^/pm/themes/
RewriteRule /themes/(.*)$ /pm/themes/$1 [L]
Now the request is rewritten only once and you're done.
You probably need to add the following lines before your RewriteRule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
It will only evaluate your rewrite rule if the requested file or directory doesn't exist.
You should post your .htaccess file so we can offer better advice

Htaccess rewrite url (Shorten url)

So I have the url: myurl.com/projects/url/visit.php?link=fbehe and I want to rewrite it as so:
myurl.com/u/fbehe
But it isn't working. I am using this so far:
RewriteEngine On
RewriteRule ^/u/([^/]*)$ visit.php?link=$1 [L]
I would also like to note that I placed my htaccess file in the directory with my visit.php file. so myurl.com/projects/url/.htaccess
How could I achieve this?
You may try this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/u/([^/]+)/?$ [NC]
RewriteRule .* projects/url/visit.php?link=%1 [L]
Maps silently
http://myurl.com/u/anyvalue with or without trailing slash
To
http://myurl.com/projects/url/visit.php?link=anyvalue
Try using NC as well. Also, not sure if the absolute path is working. Try a relative if the .htaccess is in the root of your website.
RewriteRule ^/u/([^/]*)$ visit.php?link=$1 [L,NC]
do you use any framework of this? maybe u can you there functionality like routes. can rewrite your url..
Like Codeigniter,Cake
They have routes to change the url like that.

How to re-write this URL in PHP using .htaccess?

I am working on one website.
On this website, porfile URL currently is like this:
http://eprofile.co/eprofile.php?user=degroundshaker
I want to rewrite this URL as:
http://eprofile.co/degroundshaker
This, is addon domain so its files are under a folder called "eprofile.co" in my cPanel and there is one .htaccess file.
So, i need solution and please let me know what rule i need to add and what should be the complete format in .htaccess
I m newbie in .htaccess.
Try this:
RewriteEngine On
RewriteRule ^([^/]*)$ /eprofile.php?user=$1 [L]
Try putting this in the htaccess file in your document root:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)$ /eprofile.php?user=$1 [L]
Then you need to change all of your profile links from looking like this:
http://eprofile.co/eprofile.php?user=degroundshaker
to looking like this:
http://eprofile.co/degroundshaker
EDIT:
Alternatively, you could do:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/eprofile.php
RewriteRule ^([^/]*)$ /eprofile.php?user=$1 [L]

How to use .htaccess to make a URL like http://domain.com/query

I want to make
http://domain.com/index.php?query=query
look like
http://domain.com/query
I know I need to use .htaccess, but I have no idea how to approach this.
Something like:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !index.php
RewriteBase /
RewriteRule ^(.+) /index.php?query=$1
Edited:
If you really want your URLs to look like you asked, you should host all your media files (CSS, JS and images) in another virtual host, lets say, http://media.domain.com - because can't tell the difference if "query" matches the name of an existing file on domain.com.
The keyword to search for is RewriteRule.
The Drupal .htaccess file is a good example of mapping /?q=query to /query, but not redirecting things which provide an explicit match - so /files/something.css which is a real file will not be redirected. Here's the relevant snippet from Drupal's .htaccess with ?q= changed to ?query=.
RewriteEngine On
RewriteBase /
# Rewrite URLs of the form 'index.php?query=x'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?query=$1 [L,QSA]

Resources