.htaccess RewriteRule remove some url parts - .htaccess

I'm still new in htaccess and I want to learn it if possible but I need to finish my project first in school.
My original url is:
http://localhost/example/php/admin/
I want to rewrite it so it will only shows and also that's what will be need to write in url to access the url above:
http://localhost/example/admin/
is it possible with htaccess?
also if you have a good advice where to learn htaccess, I want to learn if possible. thanks :)

Try this
RewriteRule ^php/(.*)$ /$1 [L,NC,R]

Related

Redirect, but preserve part of URL

its possible create a redirect, replace only part in the URL?
another part preserve...
in this case ONLY listing to business
EXAMPLE:
www.example.com/listing/new-york
www.example.com/listing/boston
www.example.com/listing/etc...
to
www.example.com/business/new-york
www.example.com/business/boston
www.example.com/business/etc...
thank you for tips :)
This is fairly easy to do with mod_rewrite. See this question if you need help enabling that.
Then add the following to your .htaccess
RewriteEngine on
RewriteRule ^listing/(.*)$ business/$1 [R,L]
Change the [R] flag to [R=301] after testing the redirect works as expected to turn the redirect into a permanent redirect.

How to remove middle part from URL - mod_rewrite

How to rewrite this url "www.domain.com/index.php?route=custom/static/page" to "www.domain.com/page" in htaccess file, basically just want to take out index.php?route=custom/static/ from urls.
I don't know regex so I tried http://www.generateit.net/mod-rewrite/, but it only generates
RewriteEngine On
RewriteRule ^([^/]*)$ /index.php?route=$1 [L]
which doesnt remove 'custom/static' from URLs, I tried a few other examples as well but only removes index.php? and doesnt pass variable, any help is appreciated.
Do you know the concept of using mod-rewrite?
In your question you have mentioned to use mod-rewrite to redirect
"www.domain.com/index.php?route=custom/static/page",
Here $_Get['route']="custom/static/page"] $url_parameter=$_Get['route']
to
"www.domain.com/page" [here $_Get['route']="page"],
So now you can mannually add "custom/static/" to the obtained value of $_Get['route']. as $url_parameter="custom/static"+$_Get['route'] //For PHP
Using your mod_rewrite you can fulfill your demands,
RewriteEngine On
RewriteRule ^([^/]*)$ /index.php?route=$1 [L]
But if you need out of box solution using .htaccess then I suggest learning "rewrite-engine" instead of using generating tool

.htaccess redirects: from old site with variables to Drupal & url aliases

Please look at the edits on this post. The last code examples I give are what I'm trying to accomplish. I have 147 lines of code that I'm working with. I guess I'm actually not trying to make a rule so much as I am trying to figure out redirects with those variables.
OK, I've been researching this for a few hours, and the regex apache rewrite rules get pretty specific and I'm stumped. Client has asked that the old site urls be rewritten/redirected to the new drupal site and corresponding pages that have url aliases. There are 147 variables (all 1 - 216 (out of sequence))
Example:
I need
/hardware/bikes.php?recordID=1 to redirect to http://www.bikerus.com/?q=hardware/first-bike-that-really-flies
then
/hardware/bikes.php?recordID=2 to redirect to http://www.bikerus.com/?q=hardware/second-awesome-bike-that-really-flies
and so on
This is what I have so far:
RewriteCond %{QUERY_STRING} ^recordID=([0-9]+)$
RewriteRule ^/hardware/bikes\.php?recordID=%1$ http://www.bikerus.com/?q=hardware/first-bike-that-really-flies
This is obviously not working, any ideas ladies and chaps?
Ideally, it would be great if I could figure out another problem with Drupal I haven't figured out yet (for years).
Have a redirect that goes like this:
redirect 301 /hardware/1.html http://www.bikerus.com/hardware/first-bike-that-really-flies
Instead of this:
redirect 301 /hardware/1.html http://www.bikerus.com/?q=hardware/first-bike-that-really-flies
But my first question is the priority now.
Thanks in advance.
EDITS:
The files I'm trying to redirect to aren't static, they're url aliases generated by Drupal's database.
In the end, yes, I will need to make 147 individual records (hardcoded), my question is how to do this:
redirect 301 /hardware/bikes.php?recordID=1 http://www.bikesrus.com/?q=hardware/random-bike-title-one
redirect 301 /hardware/bikes.php?recordID=2 http://www.bikesrus.com/?q=hardware/random-bike-title-two-ewhbcfn
redirect 301 /hardware/bikes.php?recordID=3 http://www.bikesrus.com/?q=hardware/random-bike-title-three-kxjhmuflr
redirect 301 /hardware/bikes.php?recordID=4 http://www.bikesrus.com/?q=hardware/random-bike-title-four-more-random-stuff
And for those that don't know, Drupal has a clean url rewrite rule that comes shipped with the .htaccess file that I'm currently editing:
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
So I need to be able to navigate this as well.
The problem to your second issue is that redirect really stinks for anything past a simple redirect. You need a RewriteRule to make this:
redirect 301 /hardware/1.html http://www.bikerus.com/?q=hardware/first-bike-that-really-flies
More like this:
RewriteRule ^([a-zA-Z]+)/$ /?q=$1 [R=301,L]
Or maybe this:
RewriteRule ^([a-zA-Z]+)/(.*)$ /?q=$1/$2 [R=301,L]
But it’s not clear in your post where you are getting the first-bike-that-really-flies stuff from. So not to clear on how to handle that in Drupal. Very specific to your setup.
Actually just answered a mod_rewrite question similar to this over here. Might give you more insight.
Just figured it out.
RewriteCond %{THE_REQUEST} recordID=1
RewriteRule . http://www.bikesrus.com/hardware/bike-that-really-flies? [R=301,L]
BOTH!! On my own, with a little more searching. The difference is that I put the word "hardcoded" into my search keywords this time. Thanks everyone who commented.

htaccess redirect issue mod rewrite

hey well I'm trying to change the url structure using the htaccess file and I'm rewriting my old structure to the new one, although I'm having some issues.
I'm using the following code
RewriteRule video-(.*)-(.*).html?$ http://mysite.com/download-$2-video-$1.html [R=301,L]
Now the problem I'm running into is that when I try to redirect something like
http://mysite.com/video-1-this-is-the-title.html
it's redirecting it to
http://mysite.com/download-this-video-1.html
instead of
http://mysite.com/download-this-is-the-title-video-1.html
if anyone could help that would be great! :)
If the first backreference is always going to be numbers, you could try changing your regular expression to:
RewriteRule video-([0-9]+)-(.+).html?$ http://mysite.com/download-$2-video-$1.html [R=301,L]

Redirect dynamic subdomain to same subdomain with subpage. How?

I'm a little stuck in here. I need to get some help with this subdomain-situation.
I need to redirect http://dynamicsubdomain.example.com/ to
http://dynamicsubdomain.example.com/account/welcome.html.
How do I do this? I tried several things but all without result. The main problem is that I can't fetch the entered dynamic subdomain from the %{HTTP_POST} string from mod_rewrite.
Another issue would be that it's creating and endless loop. So it only needs to redirect on these conditions, not when there's a URL like http://dynamicsubdomain.example.com/test/page.html. Because else it will create and endless loop. It's just for the starting page from the website.
I hope y'all can help me out, it's one of the last but important things from my project.
Thanks in advance!
There are several options on the URL redirection wiki page. For example, how about dropping an index.php in the root that redirects to the destination?
header("Location: http://dynamicsubdomain.example.com/account/welcome.html");
Why does the domain matter so much if you are staying on the same domain, and just redirecting to a different path?
The UseCanonical setting in Apache may have an effect on this, but it is defaulted to on, which preserves the host and port specified in the request.
RewriteRule ^/$ /account/welcome.html [R,L]
Hey guys, thanks for your support/help but I found the solution myself. Quicker than I thought :)
This is what does the trick, I hope I can help someone with this:
RewriteCond %{REQUEST_URI} ^/$ [NC]
RewriteCond %{HTTP_HOST} ^([A-Za-z0-9]+).example\.com [NC]
RewriteRule ^ http://%1.example.com/account/welcome.html [L]
#gahooa: I need to do this because my mainpage example.com is just a sort of landing-page with no special things. The extra part of the URL "account/welcome.html" is for showing a page related to the "subdomains"-account (gahooa.example.com for example will show your profile page). In my app I catch up the subdomain by a preg_match so it knows witch account it has to load. I hope I'm clear :) But thanks anyway!
This is my first time i'm using Stackoverflow but it actually works great! Quick replies from experts, great work! I definitely will come back :)
If you really want to use HTTP_HOST:
RewriteRule ^$ http://%{HTTP_HOST}/account/welcome.html [L,R]
But like gahooa already said you don’t specify an absolute URL if you stay with the same host. The URL path will suffice:
RewriteRule ^$ /account/welcome.html [L,R]

Resources