Rewrite within a folder - .htaccess

This is very basic but still eludes me, even after studying the relevant stackoverflow pages.
I want this
mysite.net/special/
to return this (the index here)
mysite.net/info/special/
showing the requested address in the browser, not the longer actual address.
The closest I’ve got (with this in the root htaccess) is
RewriteRule ^special/$ info/special/ {QUERY_STRING}
but it’s wrong.
I had thought this is a direct match-and-replace, but apparently not.
RewriteEngine is on.
Guidance appreciated. Thank you.

RewriteRule ^special/$ info/special/ [R=301,QSA,L]
RewriteRule Flags:
R=301: redirect permanently
QSA: combine query string
L: last rule

I am assuming index as index.php for your index file. we are rewriting to your existing url.
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/special/$
RewriteRule ^ info/special/index.php [L]

Related

force index.php in url with htaccess

I've spent the last two hours reading stackoverflow and the apache manual trying to find a solution to this, but haven't found any, if a directory is access (http://domain.ext) I'd like to force index.php to appear in the url (http://domain.ext/index.php) if possible.
If anyone could help I'd really appreciate it, thank you.
RewriteEngine on
RewriteRule index.php
you can use this:
RewriteEngine on
RewriteRule ^$ /index.php?
(but you will not see it in a browser because the rewrite engine just directs to the correct file/directory without changing the url from the browser)
with [R] tag you can redirect the user so the new url would appear in the browser
I've just found what works for me
RewriteEngine On
RewriteRule ^/?$ $1/folder/index.php$2 [R=301,L]

Redirect Specific non-existing URL to an existing

I have a simple need to redirect:
mypage.com/myurl (which doesn't not exist; meening that returns a 404 error)
To
mypage.com/newurl/myurl (which is an exitsing page with content).
I saw a tons of forum post about this, but didn't find an awnser. I tried lot of thinks but with internal server error, infinitive loops etc.
Please note that I have a www to non www permanent redirect, like this:
RewriteCond %{HTTP_HOST} ^www\.mypage\.si [NC]
RewriteRule ^(.*)$ http://mypage.si/$1 [L,R=301]
UPDATE:
I will be specific:
This URL doesn't exist: http://domodom.si/sl/nepremicnine
and needs to be redirected here: http://domodom.si/sl/nepremicnine/nepremicnine-ponudba
Hope this will offer more insights about the problem.
Thanks
assuming mod_rewrite
RedirectPermanent /myurl/ mypage.com/newurl/myurl
Add this to your .htaccess file
RewriteBase /
RewriteRule ^(.*)$ newurl/$1 [NC,L]
you may want to try implementing the answer of nwellnhof in this thread.. it worked for me..

.htaccess rules with wildcard subdomains

I have looked and keep finding really similar solutions and I managed to get it working halfway.
Solved first half:
entered url: wildcard.domain.com points to domain.com/wildcard (and doesn't change url) GREAT!
the .htaccess file code below does this.
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(?!www\.)([^.]+)\.pagebyme\.com$ [NC]
RewriteCond %{REQUEST_URI}::%1 !^/([^/]+).*?::\1
RewriteRule ^(.*)$ /%1/$1 [L]
Problem 2: if entered url is wildcard.domain.com/admin shows wildcard.domain.com/wildcard/admin
I don't want wildcard to show the second time. I just want the url to be wildcard.domain.com/admin
What rule do I need to add?
This is my first time using stackoverflow. Thanks in advance.

Change Displayed URL Structure using mod_rewrite NOT Working

I need to change the structure of the displayed client-side URL. I'm not too skilled using regex and coding for the .htaccess file. Basically, I have a structure that looks something like:
http://www.example.com/catalog/index.php?cat=lt&sec=lt1-1&id=nmlt10.
I would like this to be displayed in the address bar as:
http://www.example.com/catalog/lt/lt1-1/nmlt10.
This is what I came up with, but it has had no effect:
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/([^/]*)\$ /catalog/index.php?cat=$1&sec=$2&id=$3 [L]
I tested and removed any other rules in the .htaccess file to ensure nothing was being overwritten. I'm on a shared hosting apache server, and know that mod_rewrite is enabled, because I use it to rewrite non-www to www urls. I don't receive and 500 error messages, I just do not notice any change at all. I'm not sure where I'm going wrong here, so hopefully someone can point me in the right direction.
Finally found a solution that worked:
RewriteEngine On
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ index.php?cat=$1&sec=$2&id=$3 [QSA,L]
Appreciate LazyOne's response to get me on the right track; however, when using:
RewriteEngine On
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ index.php?cat=$1&sec=$2&id=$3 [QSA,L]
I wasn't able to following links that were already placed on the site, it treated different directories as the variables, for example, when browsing to an image or file, say:
folder/folder/image.png
It would grab "folder" - "folder" - and "image" as the variables. I can see why that was happening, if anyone has a different solution or an explanation, please let me know, I'm always willing to learn.
Since your .htaccess is in website root folder, then you should use thus rule:
RewriteEngine On
RewriteBase /
RewriteRule ^catalog/([^/]+)/([^/]+)/([^/]+)$ /catalog/index.php?cat=$1&sec=$2&id=$3 [QSA,L]
If you place it in .htaccess in /catalog/ folder, then you can remove catalog from it:
RewriteEngine On
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ index.php?cat=$1&sec=$2&id=$3 [QSA,L]
I have tested rule before posting -- works fine for me.
This rule (same as above) will check if URL is a file or folder and will only rewrite if it is not:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ index.php?cat=$1&sec=$2&id=$3 [QSA,L]

htaccess - Rewrite rule and/or condition to fix totally different discontinued or incorrect incoming link

Well, I am stuck again. Two days of reading and again, found some close solutions but, nothing fits and all my experiments failed.
This is a continuation of my question:
here at stackoverflow
The 4 rules below take my incoming links:
http://somedomain.com/getme.pl?dothis=display&partnum=1234567
and beatifies it.
Also allows users to use the beatified version right in address bar:
http://somedomain.com/1234567
Here are my working rules:
RewriteRule ^([\s]*)$ /getme.pl [L] ## in case there is a space or nothing.
RewriteRule ^([0-9]*)$ /getme.pl?dothis=display&partnum=$1&rewrite [L]
RewriteCond %{QUERY_STRING} partnum=([0-9]*)$
RewriteRule (.*) /%1? [L,R=301]
Works great but, I discovered there are some old links to the site out there:
http://somedomain.com/oldversion.php?id=123456789
And
http://somedomain.com/oldversion.php?r=86this&id=123456789
I would like to just grab the id=[0-9] and integrate it with my working rules.
I suppose, the rule would be inserted between the second and third rules above.
I tried various attempts (about 100!) like:
RewriteRule ^(oldversion\.php)?([a-z]{1})=([a-z0-9]*)&([a-z]{2})=([0-9]*)$ /$4? [L]
RewriteRule ^(oldversion\.php)?([a-z]{2})=([0-9]*)$ /$3? [L]
As you see, two days of reading and nothing is sinking in for me.
I tried several variations of the working rules I already have as well, to no avail.
Can't I just get the 123456789 off of the outdated .php urls somehow and stick it in my existing rules?
Thanks for your help and explaining down to my level co, I just might be able to understand...
Put this at the end of your .htaccess file:
RewriteCond %{QUERY_STRING} id=([^&]+)(&|$) [NC]
RewriteRule ^oldversion\.php$ /%1? [L,R=301,NC,NE]
For a URI of /oldversion.php?r=86this&id=123456789 it will internally redirect to /123456789
Remember RewriteRule just matches your URI and it cannot match your QUERY_STRING.

Resources