Remove url segment if other segment matches string .htaccess - .htaccess

I have urls like so:
http://www.domain.com/account/home
http://www.domain.com/account/settings/email
http://www.domain.com/account/widget
http://www.domain.com/account/widget/2
http://www.domain.com/account/...
I would like to rewrite the url only when widget is in the 2nd segment and for it to be rewritten without the 1st segment account like so:
http://www.domain.com/widget
http://www.domain.com/widget/2
http://www.domain.com/widget/...
How would I go about doing this? I know how to remove account entirely but not selectively.
NB account is a subfolder that my website resides in.
[EDIT]
I have found this to work, is this the best way to do it?
RewriteEngine on
RewriteRule ^account/widget/(.*)$ /widget/$1 [L]

RewriteEngine on
RewriteRule ^account/widget/(.*)$ /widget/$1 [L]

Related

how to convert exrenal link to internal using .htaccess?

i want convert all external link to internal and redirect them
let me show you what i want:
i have an external link to my website like this:
link
and i converted it to:
link
OR
link
OR somthing like this
how can i use .htaccess to make sure when someone opened the link he goes to:
https://external.com/some text here
i tried this but id didn't worked
RewriteEngine On
RewriteRule ^mywebsite.com/redirect/(.*)$ /$1 [NC,L]
RewriteRule ^mywebsite.com/redirect/(.*)$ /$1 [NC,L]
RewriteRule only matches against the path component of the URL. (And when configured in .htaccess context, the path it checks against never starts with a leading slash, the path to the current directory has been stripped off at this point already.)
/$1 would cause a slash before the actual substitution URL.
Both fixed, it should simply look like this:
RewriteRule ^redirect/(.*)$ $1 [L]

More than one htaccess rewrite rule

I'm having some trouble understanding .htaccess basically I'm trying to have more than one rewrite rule; for example I have a profile page and then I also want to rewrite the index page so how would this be done; in my previous attempts I could only use one rewrite rule perhaps I was doing something wrong; I have a profile page with the link /profile/profile.php?user=$username and the index page account.php?page=featured how could I get the profile page to look like /account/$username and the account page to look like /account/featured thankyou also how would I then add more later down the line?
the account.php file is in the root directory.
RewriteEngine On
RewriteBase /
RewriteRule ^tag/([^/]+)/([^/]+)$ /tag/index.php?hash=$1&cat=$2
Options All -Indexes
ErrorDocument 403 Denied
that is my current .htaccess which uses the hashtags and shows them like this /tag/$hashtag when I tried to copy and paste this to then use under it it didn't work.
As mentioned in my comment, Apache won't be able to tell which file to rewrite to. So you'll need to change one of the URI structures. As a recommendation, change the one for the profiles.
Here is an example to show you how to do this:
RewriteRule ^tag/([^/]+)/([^/]+)$ /tag/index.php?hash=$1&cat=$2 [L]
RewriteRule ^account/([^/]+) /account.php?page=$1 [L]
RewriteRule ^profile/([^/]+) /profile/profile.php?user=$1 [L]
Remember the [L] flag, which causes rewriting to stop when a match is found.

Need to .htaccess redirect path with a variable URI segmant to another directory without changing URL

My current .htaccess looks like this:
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^[0-9]+/pos /pos/$1 [L]
Currently this will redirect a url such as example.com/234234234/pos to example.com/pos
I would like it to load the directory example.com/pos, but without losing the original URL (example.com/234234234/pos) from the address bar.
Basically, the number listed in the url can change, but I always want it to load the same path.
This rule doesn't look right:
RewriteRule ^[0-9]+/pos /pos/$1 [L]
As you're not capturing anything hence there is no $1. If you want to ignore any number before /pos then use this rule:
RewriteRule ^[0-9]+/(pos)/?$ /$1 [L,NC]
Thank you anubhava. Part of your answer helped me. I did want the numbers ignored, but didn't want them to disappear.
This is the line I needed:
RewriteRule ^[0-9]+/pos/ /pos/ [L,NC]
Did not need to capture anything at all. That is what was causing the redirect.

rerwrite url in .htaccess

I want to make the old urls point to the new pages. I'm having trouble because the old URL looks like this: http://nilandsplace.com/store/camping_eng/coghlans-campfire-cooking-forks-toaster-forks-package-of-4.html.
I want to do a URL re-right in .ht access from mywebsite.com/camping_eng/some.html to mywebsite.com/camping/some.html, were the "some.html" part is identical.
Only camping_eng changes to camping.
You might want to try this:
RewriteEngine on
RewriteRule ^camping_eng/([a-zA-Z0-9-=_.]+)/?$ http://mywebsite.com/camping/$1 [L,R=301]
This example works according to your question:
from mywebsite.com/camping_eng/some.html
to mywebsite.com/camping/some.html were the "some.html" part is identical.
RewriteEngine on
RewriteBase /store
RewriteRule ^camping_eng/(.*)$ camping/$1 [QSA,L,R=301]

HtAccess: Basic ModRewrite

Given urls like this:
mysite.com/index.php
mysite.com/page/member/lobby.php
mysite.com/page/videos/video1.php
How can I rewrite the urls with .htaccess to hide the /page/ folder when it's present?
So the end result is:
mysite.com/index.php
mysite.com/member/lobby.php
mysite.com/videos/video1.php
You can use this rule to add page/ to your path internally:
RewriteCond $1 !=page
RewriteRule ^([^/]+)/.+ page/$0 [L]
Now every request that’s URI path has at least two path segments but that’s first segment is not page will be prefixes with /page. So /member/lobby.php will be rewritten to /page/member/lobby.php.

Resources