How to redirect using .htaccess in apache? - .htaccess

I am trying to create a user management system where users can register another users using their pre-created user links such as http www .abc .co. uk/access/register.php?id=username [sorry not allowing to put more links]
but I want the long link to be like this:
http://www.abc.co.uk/username
and when users type the short url it will re-direct to http://www.abc.co.uk/access/register.php?id=username
Below are the codes in my .htaccess file at the moment as I am also running a wordpress in the same domain.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(\w+)$ ./access/register.php?id=$1 [L,QSA]
Can the .htaccess additional codes that I have added for the username redirect be used along with other wordpress redirect in the same .htaccess file?
When I type the short url in the browser it takes me to a "page not found" page.
I have tried several ways and the above codes does not work. Could anyone please help me? Thanks
Jay

What you want is not possible. How could apache differentiate between wordpress pages and usernames?
www.abc.co.uk/contact could be your contact site or the user “contact”. You need to put your referrer urls in a subfolder, eg. register (so you get abc.co.uk/register/username:
RewriteEngine On
RewriteBase /
# Register
RewriteRule ^/register/(.+)$ ./access/register.php?id=$1 [L,QSA]
# Wordpress
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

Related

.htaccess image folder redirect from other server

So I need to do a .htaccess file that allow me to redirect the images link folder (wp-content/upload/) from a different site (http://widesigner.com.br/alessandra/) to be this one (http://www.alessandratonisi.com.br/site/)
Basically it's redirect some image links, example:
http://www.widesigner.com.br/alessandra/wp-content/uploads/2012/03/MG_6058-600x400.jpg
http://www.widesigner.com.br/alessandra/wp-content/uploads/2012/03/MG_9515.jpg
to
http://www.alessandratonisi.com.br/site/wp-content/uploads/2012/03/MG_6058-600x400.jpg
http://www.alessandratonisi.com.br/site/wp-content/uploads/2012/03/MG_9515.jpg
So what you need is redirect a website domain to another one for a specific folder. I don't know if the next line will help you to solve the problem, I don't think that will be the solution since I didn't test it.
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/alessandra/(.*)$
RewriteRule ^(.*) https://www.alessandratonisi.com.br/site/%1 [R=302,NC]
Here are a lot of examples that could lead you to the correct answer: https://gist.github.com/ScottPhillips/1721489.
UPDATE:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/alessandra/(.*)$
RewriteRule ^(.*) https://www.alessandratonisi.com.br/site/%1 [R=302,NC]
RewriteBase /site/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /site/index.php [L]
</IfModule>
# END WordPress

How to add redirect in htaccess for all urls not cotaining specific string?

I'm having some htaccess issues with my wordpress blog. My previous url's were something like
article-name.html
Now I changed the structure to
blog/article-name.html
But I'm still getting 404 for old url's I shared on various other sites. I've tried adding in htaccess a rule, also tried "redirection" plugin with no success:
and in .htaccess I tried:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^((?!blog/).)*.html$ - /blog/$1.html [L]
</IfModule>
Your regex appears to be a problem, try this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^((?!blog/).+?\.html)$ /blog/$1 [L,R=301,NC]
Change the RewiteBase
But your .htaccess is not an Original WordPress .htaccess. Why you dont use that?

htaccess rewrite rule blog links

I have been using htaccess to remove rewrite certain URLs, however I am looking at something a little more complicated at the moment.
Our website blog (WordPress) used to have links like this:
​/blog​/postname/1387
However after redoing the website our links are now currently just
/postname
Would it be possible to redirect any uses from ​/blog​/postname/1387 and get rid of the blog and number at the end via htaccess so it just contains the postname? At the moment I have:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteRule ^blog/(.*)/.*$ $1/
</IfModule>
Would love to hear any hints or tips, as this is not actually doing any redirecting, what am I doing wrong?
Let's just do a little cleanup:
<IfModule mod_rewrite.c>
#Turn on the RewriteEngine
RewriteEngine On
#Rewrites are all relative to /
RewriteBase /
#Explicit - If the request is for index.php, do nothing.
RewriteRule ^index\.php$ - [L]
#Conditional – Unless the file or directory specifically exists,
#If the request is for the old style blog URI, redirect to new style and stop.
#Otherwise, redirect to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog/(.*)/.*$ $1/ [R=301,L,QSA]
RewriteRule . /index.php [L]
</IfModule>

Redirection Subfolder Htaccess

I want a redirect using htaccess I have a post name wordpressurl/curious and also a folder named curious
What I want is if a user tries to access
wordpressurl/curious
wordpressurl/curious/
wordpressurl/curious/subfolder
note trailing slash in 1 and 2 and 3 is subdirectory. All of them should give 404 error wordpress page using htaccess only
Edited
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /Wordpress_Work/realestate/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /Wordpress_Work/realestate/index.php [L]
</IfModule>
# END WordPress
You can access the wordpress post that is the same as a curious directory by making the changes indicated below. Change curious to whatever your actual directory/post is.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /Wordpress_Work/realestate/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
#add an OR
RewriteCond %{REQUEST_FILENAME} !-d [OR]
#OR if the URI is curious or curious/ or curious/subfolder
RewriteCond %{REQUEST_URI} ^/Wordpress_Work/realestate/curious(/.*)?$
RewriteRule . /Wordpress_Work/realestate/index.php [L]
</IfModule>
If you are putting RewriteCond %{REQUEST_FILENAME} !-d you can't have same name for the page and physical directory . that's the point of using it.
Try skipping the rewritecond , though I've never tried it before . for me , I'd change the page name or add a suffix.

htaccess 301 redirect example.com/beta/xyz to example.com/xyz

I need a general rule that maps every URL in the /beta subdirectory to the corresponding URL in the root; essentially I need to remove /beta from all URLs.
In case it makes any difference, the URLs are dynamically generated by WordPress.
Currently my .htaccess file is:
# 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
Can you tell me where to put the new lines?
Thank you!
Could you try this?
RewriteEngine On
RewriteRule ^beta/(.*)$ http://example.com/$1 [R=301,L]
Your question is not entirely clear, but I would bet that what you want is having a Wordpress installed in somedir/beta appear in yoursite.com/ instead of yoursite.com/beta/. The rules you pasted are in somedir/beta/.htaccess, and are the default Wordpress rules. You must leave those alone.
What you need for that is to put the following rules in the root directory, as in, somedir/.htaccess, after changing example.com to your own domain. Your webserver first reads this root .htaccess, and when it does, it will know to rewrite requests to /beta.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/beta/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /beta/$1
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ beta/index.php [L]
</IfModule>
More info in the Codex:
https://codex.wordpress.org/Giving_WordPress_Its_Own_Directory

Resources