hi i want redirect my directory to new address and remove .html ext from end
example:
http://www.mysite.com/viewdownload/11-e/652-elena-2010.html
redirect to
http://www.mysite.com/download/viewdownload/11-e/652-elena-2010
or
http://www.mysite.com/viewdownload/9-b/1281-birdsong-2012.html
redirect to
http://www.mysite.com/download/viewdownload/9-b/1281-birdsong-2012
Assuming mod_rewrite is installed, just use URL rewriting in your .htaccess files. You can use URL rewriting to redirect users as well as doing an internal transfer.
A good reference is here: http://httpd.apache.org/docs/current/mod/mod_rewrite.html
Note that if there is any custom logic to mapping your old URIs to your new ones you might want to rewrite the old URIs to a PHP script which then returns the needed file.
Related
I'm trying to understand the difference between a rewrite and a redirect and I'm a bit confused. I've read in some places that the difference is "A Redirect rule instructs the client (usually a browser) to switch URLs and navigate to the destination of the rule. Redirect rules are typically used for old paths that you’d like to redirect to new ones.
In contrast, a Rewrite rule does not change the original URL; it simply serves the content of the rule destination at the original path. The browser can not tell that the content was served from a different path or URL, making it possible to display content from a different path or URL on any other path on your site. "
But looking into Apache Servers, the documentation says this:
"The mod_rewrite module uses a rule-based rewriting engine, based on a PCRE regular-expression parser, to rewrite requested URLs on the fly. By default, mod_rewrite maps a URL to a filesystem path. However, it can also be used to redirect one URL to another URL, or to invoke an internal proxy fetch."
So in this case can rewrites function as redirect such that the URL will appear to change in the browser? Or is it always the case that if it's a rewrite, the browser url will never change? Is it a different answer if you're using IIS vs Apache Server perhaps?
URL rewrite rule uses to create user-friendly URLs that are easier for users to remember and easier for search engines to find.
The rewrite module for IIS can be used to execute 2 actions:
rewrite => the URL stays the same but the content is loaded from somewhere else
<action type="Rewrite" url="http://www.{HTTP_HOST}/{R:0}" />
redirects => when the user’s browser is taken to a new URL.
<action type="Redirect" url="http://www.{HTTP_HOST}/{R:0}" />
Both use the same rules and conditions to determine if the action should be triggered or not.
same for apache but in apache system wi;l be different for redirect it uses "redirect|R" flag.
Reference link:
https://weblogs.asp.net/owscott/rewrite-vs-redirect-what-s-the-difference
I have set up a new store system on a web site. The old one uses static HTML files, while the new one uses JS combined with a JSON file that has all the information in it for the store. I am going to make a page that will take the filename, and convert it to the new URL format. For example, https://example.com/store/products/tremendous.html is now https://example.com/store/store.html?pageType=item&page=store-tremendous (or whatever the ID is, replacing store-tremendous). How would I use the .htaccess file to redirect /store/products/tremendous.html to /redirect.html?page=tremendous.html, and so on, for all the files in the products directory?
EDIT: I figured it out, it's really simple. See my comment. I'll mark as resolved once I am able to.
I figured it out, it's actually really simple. Here's the relevant part of my .htaccess:
Redirect 301 /store/products/ /redirect?page=
Redirect 301 /store/accessories/ /redirect?page=
This will redirect any page in either of those folders to my redirect script, while appending the filename to the end of the URL.
I am able to reach my website at a certain ip address and I am going to implement a REST service. I have some PHP files that perform actions on a database and I am calling them from the client. I am using linux ubuntu as server and so far I can do this:
http://xxx.xxx.xxx.xxx/api/create/?id=someId&val=someValue
http://xxx.xxx.xxx.xxx/api/delete/?id=someId
I can do the above because inside /var/www/html I have a folder called api that contains another folder called create. The create cointains the file index.php so that I can omit it and execute the URL you can see above.
This works fine but I don't think this is the proper way to do it. I am new with this so I don't know what to do. After some researches I have found that my goal probably be achieved using an .htaccess file use url rewriting but I am not sure.
How can I do this? Do I have to place all the php files in a single folder and then use an htaccess file? (^)
(^) To be more precise: instead of having this
http://xxx.xxx.xxx.xxx/api/create/index.php?id=someId&val=someValue
http://xxx.xxx.xxx.xxx/api/delete/index.php?id=someId
//and so on with other actions...
Do I have to create a folder like
http://xxx.xxx.xxx.xxx/files/
containing all my php files (create.php, delete.php, view.php...) and the use an htaccess to redirect?
I see that websites offer their api using www.domain.com/api/something/?data=Value or www.domain.com/api/something/dataAbout/. Are they doing what I have said about the .htaccess? I hope I have well explained my problem.
htaccess:
RewriteEngine On
RewriteRule ^api/([\w-]+)/?$ files/$1.php [L,NC]
This is inside /var/www/html and I have api inside /home/username/api .
Thanks Emma
Do it like this:
Create php files in a folder files/ subdirectory as create.php, delete.php, view.php etc (by renaming each individual index.php file, you mentioned).
Move away api directory somewhere outside site root.
Once that is done use following .htaccess file in /var/www/html/:
RewriteEngine On
RewriteRule ^api/([\w-]+)/?$ files/$1.php [L,NC]
Then use new URLs as:
http://xxx.xxx.xxx.xxx/api/create?id=someId&val=someValue
http://xxx.xxx.xxx.xxx/api/delete?id=someId
In first, this is not the right way to create a RESTFUL API. My suggestion is to you read a best practices article.
You shouldn't create a CREATE and DELETE folder. You should use HTTP actions.
To create a new record you should use POST. In example, POST /user and in the body you pass the user's information.
In another example, you could use the same route by using different HTTP methods: DELETE /user/1 to delete a user and PATCH /user/1 to edit some already existent user's information.
Hope it's help you.
I want to shorten my URI to just my site address that is
http://www.abcdef.org/index.php?pg=23
http://www.abcdef.org/histop.php?pg=1
http://www.abcdef.org/other.php?pg=29
to
www.abcdef.org
How can I rewrite this using my `.htaccess file? I read a lot of articles but could not find any solution.
Use Redirect directive
Redirect /foo.html /bar.html
Apache Documentation
But understand when the user types in whatever URL it will map to your index file in your case. But you can't just hide every URL to the domain name...it doesn't work that way.
I'm trying to create a redirect in-between page of sorts, because the URL that I'm redirecting TO includes more information than the URL I'm redirecting FROM. I'm using a short domain (hrci.me) with an htaccess file to redirect to the full domain (currently reachchallenges.infectionist.com). An example would be:
hrci.me/ch123
The path, ch123, includes the identifier that lets me know it's a challenge link (ch), and the 123 is the challenge ID. Each challenge has a title that I like to append to the end of the URL for SEO purposes. This example URL would redirect to:
reachchallenges.infectionist.com/challenge/123/Challenge+Title
The "Challenge+Title" part is stored in the database and needs to be retrieved by the challenge id, so I wrote a simple PHP script that does just that and then handles the redirect itself. My htaccess rule looks like this:
RewriteRule ^ch([0-9]{1,4})(/)?$ redirhandler.php?chid=$1 [L]
So the request to /ch123 should redirect to redirhandler.php?chid=123, which would get the title then redirect to the other domain at /challenge/123/Challenge+Title. The problem is, the short domain is set up to forward all incoming requests to the long domain, maintaining the original path (so hrci.me/something would redirect to reachchallenges.infectionist.com/something), and I'm finding that after the htaccess handles the rewrite to redirhandler.php, it then redirects that to reachchallenges.infectionist.com/redirhandler.php...
Basically, I need it to ignore any further redirects if the path is redirhandler.php, allowing the php script to handle the rest o the redirect. I'm thinking a RewriteCond is how I might do this, but I can't figure it out.
It sounds like your rule that forwards all incoming requests to the long domain is higher up in the .htaccess file than the more specific rule for /ch* requests. Try putting the more specific rule before the more general one.